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 | 0dc9e6d | 2022-08-01 11:55:57 +0200 | [diff] [blame] | 52 | #include <import/ebmbtree.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 54 | #include <haproxy/api.h> |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 55 | #include <haproxy/applet.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 56 | #include <haproxy/chunk.h> |
Willy Tarreau | 7c18b54 | 2020-06-11 09:23:02 +0200 | [diff] [blame] | 57 | #include <haproxy/dgram.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 58 | #include <haproxy/global.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 59 | #include <haproxy/hlua.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 60 | #include <haproxy/listener.h> |
Willy Tarreau | 7a00efb | 2020-06-02 17:02:59 +0200 | [diff] [blame] | 61 | #include <haproxy/namespace.h> |
Christopher Faulet | 9553de7 | 2021-02-26 09:12:50 +0100 | [diff] [blame] | 62 | #include <haproxy/net_helper.h> |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 63 | #include <haproxy/protocol.h> |
Willy Tarreau | 8f6da64 | 2023-03-10 12:04:02 +0100 | [diff] [blame] | 64 | #include <haproxy/quic_sock.h> |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 65 | #include <haproxy/resolvers.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 66 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 67 | #include <haproxy/sock.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 68 | #include <haproxy/ssl_sock.h> |
William Lallemand | 3aeb3f9 | 2021-08-21 23:59:56 +0200 | [diff] [blame] | 69 | #include <haproxy/ssl_utils.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 70 | #include <haproxy/stconn.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 71 | #include <haproxy/task.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 72 | #include <haproxy/tools.h> |
Willy Tarreau | 08093cc | 2022-11-24 08:09:12 +0100 | [diff] [blame] | 73 | #include <haproxy/xxhash.h> |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 74 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 75 | /* This macro returns false if the test __x is false. Many |
| 76 | * of the following parsing function must be abort the processing |
| 77 | * if it returns 0, so this macro is useful for writing light code. |
| 78 | */ |
| 79 | #define RET0_UNLESS(__x) do { if (!(__x)) return 0; } while (0) |
| 80 | |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 81 | /* Define the number of line of hash_word */ |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 82 | #define NB_L_HASH_WORD 15 |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 83 | |
Willy Tarreau | 08093cc | 2022-11-24 08:09:12 +0100 | [diff] [blame] | 84 | /* return the hash of a string and length for a given key. All keys are valid. */ |
| 85 | #define HA_ANON(key, str, len) (XXH32(str, len, key) & 0xFFFFFF) |
| 86 | |
Willy Tarreau | 56adcf2 | 2012-12-23 18:00:29 +0100 | [diff] [blame] | 87 | /* enough to store NB_ITOA_STR integers of : |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 88 | * 2^64-1 = 18446744073709551615 or |
| 89 | * -2^63 = -9223372036854775808 |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 90 | * |
| 91 | * The HTML version needs room for adding the 25 characters |
| 92 | * '<span class="rls"></span>' around digits at positions 3N+1 in order |
| 93 | * 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] | 94 | */ |
Christopher Faulet | 99bca65 | 2017-11-14 16:47:26 +0100 | [diff] [blame] | 95 | THREAD_LOCAL char itoa_str[NB_ITOA_STR][171]; |
| 96 | 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] | 97 | |
Willy Tarreau | 588297f | 2014-06-16 15:16:40 +0200 | [diff] [blame] | 98 | /* sometimes we'll need to quote strings (eg: in stats), and we don't expect |
| 99 | * to quote strings larger than a max configuration line. |
| 100 | */ |
Christopher Faulet | 99bca65 | 2017-11-14 16:47:26 +0100 | [diff] [blame] | 101 | THREAD_LOCAL char quoted_str[NB_QSTR][QSTR_SIZE + 1]; |
| 102 | THREAD_LOCAL int quoted_idx = 0; |
Willy Tarreau | 588297f | 2014-06-16 15:16:40 +0200 | [diff] [blame] | 103 | |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 104 | /* thread-local PRNG state. It's modified to start from a different sequence |
| 105 | * on all threads upon startup. It must not be used or anything beyond getting |
| 106 | * statistical values as it's 100% predictable. |
| 107 | */ |
| 108 | THREAD_LOCAL unsigned int statistical_prng_state = 2463534242U; |
| 109 | |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 110 | /* set to true if this is a static build */ |
| 111 | int build_is_static = 0; |
| 112 | |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 113 | /* A global static table to store hashed words */ |
| 114 | static THREAD_LOCAL char hash_word[NB_L_HASH_WORD][20]; |
| 115 | static THREAD_LOCAL int index_hash = 0; |
| 116 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 117 | /* |
William Lallemand | e7340ec | 2012-01-24 11:15:39 +0100 | [diff] [blame] | 118 | * unsigned long long ASCII representation |
| 119 | * |
| 120 | * return the last char '\0' or NULL if no enough |
| 121 | * space in dst |
| 122 | */ |
| 123 | char *ulltoa(unsigned long long n, char *dst, size_t size) |
| 124 | { |
| 125 | int i = 0; |
| 126 | char *res; |
| 127 | |
| 128 | switch(n) { |
| 129 | case 1ULL ... 9ULL: |
| 130 | i = 0; |
| 131 | break; |
| 132 | |
| 133 | case 10ULL ... 99ULL: |
| 134 | i = 1; |
| 135 | break; |
| 136 | |
| 137 | case 100ULL ... 999ULL: |
| 138 | i = 2; |
| 139 | break; |
| 140 | |
| 141 | case 1000ULL ... 9999ULL: |
| 142 | i = 3; |
| 143 | break; |
| 144 | |
| 145 | case 10000ULL ... 99999ULL: |
| 146 | i = 4; |
| 147 | break; |
| 148 | |
| 149 | case 100000ULL ... 999999ULL: |
| 150 | i = 5; |
| 151 | break; |
| 152 | |
| 153 | case 1000000ULL ... 9999999ULL: |
| 154 | i = 6; |
| 155 | break; |
| 156 | |
| 157 | case 10000000ULL ... 99999999ULL: |
| 158 | i = 7; |
| 159 | break; |
| 160 | |
| 161 | case 100000000ULL ... 999999999ULL: |
| 162 | i = 8; |
| 163 | break; |
| 164 | |
| 165 | case 1000000000ULL ... 9999999999ULL: |
| 166 | i = 9; |
| 167 | break; |
| 168 | |
| 169 | case 10000000000ULL ... 99999999999ULL: |
| 170 | i = 10; |
| 171 | break; |
| 172 | |
| 173 | case 100000000000ULL ... 999999999999ULL: |
| 174 | i = 11; |
| 175 | break; |
| 176 | |
| 177 | case 1000000000000ULL ... 9999999999999ULL: |
| 178 | i = 12; |
| 179 | break; |
| 180 | |
| 181 | case 10000000000000ULL ... 99999999999999ULL: |
| 182 | i = 13; |
| 183 | break; |
| 184 | |
| 185 | case 100000000000000ULL ... 999999999999999ULL: |
| 186 | i = 14; |
| 187 | break; |
| 188 | |
| 189 | case 1000000000000000ULL ... 9999999999999999ULL: |
| 190 | i = 15; |
| 191 | break; |
| 192 | |
| 193 | case 10000000000000000ULL ... 99999999999999999ULL: |
| 194 | i = 16; |
| 195 | break; |
| 196 | |
| 197 | case 100000000000000000ULL ... 999999999999999999ULL: |
| 198 | i = 17; |
| 199 | break; |
| 200 | |
| 201 | case 1000000000000000000ULL ... 9999999999999999999ULL: |
| 202 | i = 18; |
| 203 | break; |
| 204 | |
| 205 | case 10000000000000000000ULL ... ULLONG_MAX: |
| 206 | i = 19; |
| 207 | break; |
| 208 | } |
| 209 | if (i + 2 > size) // (i + 1) + '\0' |
| 210 | return NULL; // too long |
| 211 | res = dst + i + 1; |
| 212 | *res = '\0'; |
| 213 | for (; i >= 0; i--) { |
| 214 | dst[i] = n % 10ULL + '0'; |
| 215 | n /= 10ULL; |
| 216 | } |
| 217 | return res; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * unsigned long ASCII representation |
| 222 | * |
| 223 | * return the last char '\0' or NULL if no enough |
| 224 | * space in dst |
| 225 | */ |
| 226 | char *ultoa_o(unsigned long n, char *dst, size_t size) |
| 227 | { |
| 228 | int i = 0; |
| 229 | char *res; |
| 230 | |
| 231 | switch (n) { |
| 232 | case 0U ... 9UL: |
| 233 | i = 0; |
| 234 | break; |
| 235 | |
| 236 | case 10U ... 99UL: |
| 237 | i = 1; |
| 238 | break; |
| 239 | |
| 240 | case 100U ... 999UL: |
| 241 | i = 2; |
| 242 | break; |
| 243 | |
| 244 | case 1000U ... 9999UL: |
| 245 | i = 3; |
| 246 | break; |
| 247 | |
| 248 | case 10000U ... 99999UL: |
| 249 | i = 4; |
| 250 | break; |
| 251 | |
| 252 | case 100000U ... 999999UL: |
| 253 | i = 5; |
| 254 | break; |
| 255 | |
| 256 | case 1000000U ... 9999999UL: |
| 257 | i = 6; |
| 258 | break; |
| 259 | |
| 260 | case 10000000U ... 99999999UL: |
| 261 | i = 7; |
| 262 | break; |
| 263 | |
| 264 | case 100000000U ... 999999999UL: |
| 265 | i = 8; |
| 266 | break; |
| 267 | #if __WORDSIZE == 32 |
| 268 | |
| 269 | case 1000000000ULL ... ULONG_MAX: |
| 270 | i = 9; |
| 271 | break; |
| 272 | |
| 273 | #elif __WORDSIZE == 64 |
| 274 | |
| 275 | case 1000000000ULL ... 9999999999UL: |
| 276 | i = 9; |
| 277 | break; |
| 278 | |
| 279 | case 10000000000ULL ... 99999999999UL: |
| 280 | i = 10; |
| 281 | break; |
| 282 | |
| 283 | case 100000000000ULL ... 999999999999UL: |
| 284 | i = 11; |
| 285 | break; |
| 286 | |
| 287 | case 1000000000000ULL ... 9999999999999UL: |
| 288 | i = 12; |
| 289 | break; |
| 290 | |
| 291 | case 10000000000000ULL ... 99999999999999UL: |
| 292 | i = 13; |
| 293 | break; |
| 294 | |
| 295 | case 100000000000000ULL ... 999999999999999UL: |
| 296 | i = 14; |
| 297 | break; |
| 298 | |
| 299 | case 1000000000000000ULL ... 9999999999999999UL: |
| 300 | i = 15; |
| 301 | break; |
| 302 | |
| 303 | case 10000000000000000ULL ... 99999999999999999UL: |
| 304 | i = 16; |
| 305 | break; |
| 306 | |
| 307 | case 100000000000000000ULL ... 999999999999999999UL: |
| 308 | i = 17; |
| 309 | break; |
| 310 | |
| 311 | case 1000000000000000000ULL ... 9999999999999999999UL: |
| 312 | i = 18; |
| 313 | break; |
| 314 | |
| 315 | case 10000000000000000000ULL ... ULONG_MAX: |
| 316 | i = 19; |
| 317 | break; |
| 318 | |
| 319 | #endif |
| 320 | } |
| 321 | if (i + 2 > size) // (i + 1) + '\0' |
| 322 | return NULL; // too long |
| 323 | res = dst + i + 1; |
| 324 | *res = '\0'; |
| 325 | for (; i >= 0; i--) { |
| 326 | dst[i] = n % 10U + '0'; |
| 327 | n /= 10U; |
| 328 | } |
| 329 | return res; |
| 330 | } |
| 331 | |
| 332 | /* |
| 333 | * signed long ASCII representation |
| 334 | * |
| 335 | * return the last char '\0' or NULL if no enough |
| 336 | * space in dst |
| 337 | */ |
| 338 | char *ltoa_o(long int n, char *dst, size_t size) |
| 339 | { |
| 340 | char *pos = dst; |
| 341 | |
| 342 | if (n < 0) { |
| 343 | if (size < 3) |
| 344 | return NULL; // min size is '-' + digit + '\0' but another test in ultoa |
| 345 | *pos = '-'; |
| 346 | pos++; |
| 347 | dst = ultoa_o(-n, pos, size - 1); |
| 348 | } else { |
| 349 | dst = ultoa_o(n, dst, size); |
| 350 | } |
| 351 | return dst; |
| 352 | } |
| 353 | |
| 354 | /* |
| 355 | * signed long long ASCII representation |
| 356 | * |
| 357 | * return the last char '\0' or NULL if no enough |
| 358 | * space in dst |
| 359 | */ |
| 360 | char *lltoa(long long n, char *dst, size_t size) |
| 361 | { |
| 362 | char *pos = dst; |
| 363 | |
| 364 | if (n < 0) { |
| 365 | if (size < 3) |
| 366 | return NULL; // min size is '-' + digit + '\0' but another test in ulltoa |
| 367 | *pos = '-'; |
| 368 | pos++; |
| 369 | dst = ulltoa(-n, pos, size - 1); |
| 370 | } else { |
| 371 | dst = ulltoa(n, dst, size); |
| 372 | } |
| 373 | return dst; |
| 374 | } |
| 375 | |
| 376 | /* |
| 377 | * write a ascii representation of a unsigned into dst, |
| 378 | * return a pointer to the last character |
| 379 | * Pad the ascii representation with '0', using size. |
| 380 | */ |
| 381 | char *utoa_pad(unsigned int n, char *dst, size_t size) |
| 382 | { |
| 383 | int i = 0; |
| 384 | char *ret; |
| 385 | |
| 386 | switch(n) { |
| 387 | case 0U ... 9U: |
| 388 | i = 0; |
| 389 | break; |
| 390 | |
| 391 | case 10U ... 99U: |
| 392 | i = 1; |
| 393 | break; |
| 394 | |
| 395 | case 100U ... 999U: |
| 396 | i = 2; |
| 397 | break; |
| 398 | |
| 399 | case 1000U ... 9999U: |
| 400 | i = 3; |
| 401 | break; |
| 402 | |
| 403 | case 10000U ... 99999U: |
| 404 | i = 4; |
| 405 | break; |
| 406 | |
| 407 | case 100000U ... 999999U: |
| 408 | i = 5; |
| 409 | break; |
| 410 | |
| 411 | case 1000000U ... 9999999U: |
| 412 | i = 6; |
| 413 | break; |
| 414 | |
| 415 | case 10000000U ... 99999999U: |
| 416 | i = 7; |
| 417 | break; |
| 418 | |
| 419 | case 100000000U ... 999999999U: |
| 420 | i = 8; |
| 421 | break; |
| 422 | |
| 423 | case 1000000000U ... 4294967295U: |
| 424 | i = 9; |
| 425 | break; |
| 426 | } |
| 427 | if (i + 2 > size) // (i + 1) + '\0' |
| 428 | return NULL; // too long |
Aurelien DARRAGON | e3177af | 2022-11-22 11:42:07 +0100 | [diff] [blame] | 429 | i = size - 2; // padding - '\0' |
William Lallemand | e7340ec | 2012-01-24 11:15:39 +0100 | [diff] [blame] | 430 | |
| 431 | ret = dst + i + 1; |
| 432 | *ret = '\0'; |
| 433 | for (; i >= 0; i--) { |
| 434 | dst[i] = n % 10U + '0'; |
| 435 | n /= 10U; |
| 436 | } |
| 437 | return ret; |
| 438 | } |
| 439 | |
| 440 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 441 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 442 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 443 | * (excluding the terminating zero). |
| 444 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 445 | * long, uses only registers, and consumes only 4 cycles per char. |
| 446 | */ |
| 447 | int strlcpy2(char *dst, const char *src, int size) |
| 448 | { |
| 449 | char *orig = dst; |
| 450 | if (size) { |
| 451 | while (--size && (*dst = *src)) { |
| 452 | src++; dst++; |
| 453 | } |
| 454 | *dst = 0; |
| 455 | } |
| 456 | return dst - orig; |
| 457 | } |
| 458 | |
| 459 | /* |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 460 | * This function simply returns a locally allocated string containing |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 461 | * the ascii representation for number 'n' in decimal. |
| 462 | */ |
Emeric Brun | 3a7fce5 | 2010-01-04 14:54:38 +0100 | [diff] [blame] | 463 | char *ultoa_r(unsigned long n, char *buffer, int size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 464 | { |
| 465 | char *pos; |
| 466 | |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 467 | pos = buffer + size - 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 468 | *pos-- = '\0'; |
| 469 | |
| 470 | do { |
| 471 | *pos-- = '0' + n % 10; |
| 472 | n /= 10; |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 473 | } while (n && pos >= buffer); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 474 | return pos + 1; |
| 475 | } |
| 476 | |
Willy Tarreau | 91092e5 | 2007-10-25 16:58:42 +0200 | [diff] [blame] | 477 | /* |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 478 | * This function simply returns a locally allocated string containing |
Thierry FOURNIER | 763a5d8 | 2015-07-06 23:09:52 +0200 | [diff] [blame] | 479 | * the ascii representation for number 'n' in decimal. |
| 480 | */ |
| 481 | char *lltoa_r(long long int in, char *buffer, int size) |
| 482 | { |
| 483 | char *pos; |
| 484 | int neg = 0; |
| 485 | unsigned long long int n; |
| 486 | |
| 487 | pos = buffer + size - 1; |
| 488 | *pos-- = '\0'; |
| 489 | |
| 490 | if (in < 0) { |
| 491 | neg = 1; |
| 492 | n = -in; |
| 493 | } |
| 494 | else |
| 495 | n = in; |
| 496 | |
| 497 | do { |
| 498 | *pos-- = '0' + n % 10; |
| 499 | n /= 10; |
| 500 | } while (n && pos >= buffer); |
| 501 | if (neg && pos > buffer) |
| 502 | *pos-- = '-'; |
| 503 | return pos + 1; |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * This function simply returns a locally allocated string containing |
Thierry FOURNIER | 1480bd8 | 2015-06-06 19:14:59 +0200 | [diff] [blame] | 508 | * the ascii representation for signed number 'n' in decimal. |
| 509 | */ |
| 510 | char *sltoa_r(long n, char *buffer, int size) |
| 511 | { |
| 512 | char *pos; |
| 513 | |
| 514 | if (n >= 0) |
| 515 | return ultoa_r(n, buffer, size); |
| 516 | |
| 517 | pos = ultoa_r(-n, buffer + 1, size - 1) - 1; |
| 518 | *pos = '-'; |
| 519 | return pos; |
| 520 | } |
| 521 | |
| 522 | /* |
| 523 | * This function simply returns a locally allocated string containing |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 524 | * the ascii representation for number 'n' in decimal, formatted for |
| 525 | * HTML output with tags to create visual grouping by 3 digits. The |
| 526 | * output needs to support at least 171 characters. |
| 527 | */ |
| 528 | const char *ulltoh_r(unsigned long long n, char *buffer, int size) |
| 529 | { |
| 530 | char *start; |
| 531 | int digit = 0; |
| 532 | |
| 533 | start = buffer + size; |
| 534 | *--start = '\0'; |
| 535 | |
| 536 | do { |
| 537 | if (digit == 3 && start >= buffer + 7) |
| 538 | memcpy(start -= 7, "</span>", 7); |
| 539 | |
| 540 | if (start >= buffer + 1) { |
| 541 | *--start = '0' + n % 10; |
| 542 | n /= 10; |
| 543 | } |
| 544 | |
| 545 | if (digit == 3 && start >= buffer + 18) |
| 546 | memcpy(start -= 18, "<span class=\"rls\">", 18); |
| 547 | |
| 548 | if (digit++ == 3) |
| 549 | digit = 1; |
| 550 | } while (n && start > buffer); |
| 551 | return start; |
| 552 | } |
| 553 | |
| 554 | /* |
Willy Tarreau | 91092e5 | 2007-10-25 16:58:42 +0200 | [diff] [blame] | 555 | * This function simply returns a locally allocated string containing the ascii |
| 556 | * representation for number 'n' in decimal, unless n is 0 in which case it |
| 557 | * returns the alternate string (or an empty string if the alternate string is |
| 558 | * NULL). It use is intended for limits reported in reports, where it's |
| 559 | * desirable not to display anything if there is no limit. Warning! it shares |
| 560 | * the same vector as ultoa_r(). |
| 561 | */ |
| 562 | const char *limit_r(unsigned long n, char *buffer, int size, const char *alt) |
| 563 | { |
| 564 | return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : ""); |
| 565 | } |
| 566 | |
Willy Tarreau | 56d1d8d | 2021-05-08 10:28:53 +0200 | [diff] [blame] | 567 | /* Trims the first "%f" float in a string to its minimum number of digits after |
| 568 | * the decimal point by trimming trailing zeroes, even dropping the decimal |
| 569 | * point if not needed. The string is in <buffer> of length <len>, and the |
| 570 | * number is expected to start at or after position <num_start> (the first |
| 571 | * point appearing there is considered). A NUL character is always placed at |
| 572 | * the end if some trimming occurs. The new buffer length is returned. |
| 573 | */ |
| 574 | size_t flt_trim(char *buffer, size_t num_start, size_t len) |
| 575 | { |
| 576 | char *end = buffer + len; |
| 577 | char *p = buffer + num_start; |
| 578 | char *trim; |
| 579 | |
| 580 | do { |
| 581 | if (p >= end) |
| 582 | return len; |
| 583 | trim = p++; |
| 584 | } while (*trim != '.'); |
| 585 | |
| 586 | /* For now <trim> is on the decimal point. Let's look for any other |
| 587 | * meaningful digit after it. |
| 588 | */ |
| 589 | while (p < end) { |
| 590 | if (*p++ != '0') |
| 591 | trim = p; |
| 592 | } |
| 593 | |
| 594 | if (trim < end) |
| 595 | *trim = 0; |
| 596 | |
| 597 | return trim - buffer; |
| 598 | } |
| 599 | |
Willy Tarreau | ae03d26 | 2021-05-08 07:35:00 +0200 | [diff] [blame] | 600 | /* |
| 601 | * This function simply returns a locally allocated string containing |
| 602 | * the ascii representation for number 'n' in decimal with useless trailing |
| 603 | * zeroes trimmed. |
| 604 | */ |
| 605 | char *ftoa_r(double n, char *buffer, int size) |
| 606 | { |
| 607 | flt_trim(buffer, 0, snprintf(buffer, size, "%f", n)); |
| 608 | return buffer; |
| 609 | } |
| 610 | |
Willy Tarreau | 588297f | 2014-06-16 15:16:40 +0200 | [diff] [blame] | 611 | /* returns a locally allocated string containing the quoted encoding of the |
| 612 | * input string. The output may be truncated to QSTR_SIZE chars, but it is |
| 613 | * guaranteed that the string will always be properly terminated. Quotes are |
| 614 | * encoded by doubling them as is commonly done in CSV files. QSTR_SIZE must |
| 615 | * always be at least 4 chars. |
| 616 | */ |
| 617 | const char *qstr(const char *str) |
| 618 | { |
| 619 | char *ret = quoted_str[quoted_idx]; |
| 620 | char *p, *end; |
| 621 | |
| 622 | if (++quoted_idx >= NB_QSTR) |
| 623 | quoted_idx = 0; |
| 624 | |
| 625 | p = ret; |
| 626 | end = ret + QSTR_SIZE; |
| 627 | |
| 628 | *p++ = '"'; |
| 629 | |
| 630 | /* always keep 3 chars to support passing "" and the ending " */ |
| 631 | while (*str && p < end - 3) { |
| 632 | if (*str == '"') { |
| 633 | *p++ = '"'; |
| 634 | *p++ = '"'; |
| 635 | } |
| 636 | else |
| 637 | *p++ = *str; |
| 638 | str++; |
| 639 | } |
| 640 | *p++ = '"'; |
| 641 | return ret; |
| 642 | } |
| 643 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 644 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 645 | * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero. |
| 646 | * |
| 647 | * It looks like this one would be a good candidate for inlining, but this is |
| 648 | * not interesting because it around 35 bytes long and often called multiple |
| 649 | * times within the same function. |
| 650 | */ |
| 651 | int ishex(char s) |
| 652 | { |
| 653 | s -= '0'; |
| 654 | if ((unsigned char)s <= 9) |
| 655 | return 1; |
| 656 | s -= 'A' - '0'; |
| 657 | if ((unsigned char)s <= 5) |
| 658 | return 1; |
| 659 | s -= 'a' - 'A'; |
| 660 | if ((unsigned char)s <= 5) |
| 661 | return 1; |
| 662 | return 0; |
| 663 | } |
| 664 | |
Willy Tarreau | 3ca1a88 | 2015-01-15 18:43:49 +0100 | [diff] [blame] | 665 | /* rounds <i> down to the closest value having max 2 digits */ |
| 666 | unsigned int round_2dig(unsigned int i) |
| 667 | { |
| 668 | unsigned int mul = 1; |
| 669 | |
| 670 | while (i >= 100) { |
| 671 | i /= 10; |
| 672 | mul *= 10; |
| 673 | } |
| 674 | return i * mul; |
| 675 | } |
| 676 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 677 | /* |
| 678 | * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an |
| 679 | * invalid character is found, a pointer to it is returned. If everything is |
| 680 | * fine, NULL is returned. |
| 681 | */ |
| 682 | const char *invalid_char(const char *name) |
| 683 | { |
| 684 | if (!*name) |
| 685 | return name; |
| 686 | |
| 687 | while (*name) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 688 | if (!isalnum((unsigned char)*name) && *name != '.' && *name != ':' && |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 689 | *name != '_' && *name != '-') |
| 690 | return name; |
| 691 | name++; |
| 692 | } |
| 693 | return NULL; |
| 694 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 695 | |
| 696 | /* |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 697 | * Checks <name> for invalid characters. Valid chars are [_.-] and those |
| 698 | * accepted by <f> function. |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 699 | * If an invalid character is found, a pointer to it is returned. |
| 700 | * If everything is fine, NULL is returned. |
| 701 | */ |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 702 | 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] | 703 | |
| 704 | if (!*name) |
| 705 | return name; |
| 706 | |
| 707 | while (*name) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 708 | if (!f((unsigned char)*name) && *name != '.' && |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 709 | *name != '_' && *name != '-') |
| 710 | return name; |
| 711 | |
| 712 | name++; |
| 713 | } |
| 714 | |
| 715 | return NULL; |
| 716 | } |
| 717 | |
| 718 | /* |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 719 | * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_.-]. |
| 720 | * If an invalid character is found, a pointer to it is returned. |
| 721 | * If everything is fine, NULL is returned. |
| 722 | */ |
| 723 | const char *invalid_domainchar(const char *name) { |
| 724 | return __invalid_char(name, isalnum); |
| 725 | } |
| 726 | |
| 727 | /* |
| 728 | * Checks <name> for invalid characters. Valid chars are [A-Za-z_.-]. |
| 729 | * If an invalid character is found, a pointer to it is returned. |
| 730 | * If everything is fine, NULL is returned. |
| 731 | */ |
| 732 | const char *invalid_prefix_char(const char *name) { |
Thierry Fournier | f7b7c3e | 2018-03-26 11:54:39 +0200 | [diff] [blame] | 733 | return __invalid_char(name, isalnum); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 734 | } |
| 735 | |
| 736 | /* |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 737 | * converts <str> to a struct sockaddr_storage* provided by the caller. The |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 738 | * caller must have zeroed <sa> first, and may have set sa->ss_family to force |
| 739 | * parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then |
| 740 | * the function tries to guess the address family from the syntax. If the |
| 741 | * 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] | 742 | * string is assumed to contain only an address, no port. The address can be a |
| 743 | * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to |
| 744 | * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved. |
| 745 | * The return address will only have the address family and the address set, |
| 746 | * 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] | 747 | * The IPv6 '::' address is IN6ADDR_ANY. If <resolve> is non-zero, the hostname |
| 748 | * is resolved, otherwise only IP addresses are resolved, and anything else |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 749 | * returns NULL. If the address contains a port, this one is preserved. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 750 | */ |
Thierry FOURNIER | 58639a0 | 2014-11-25 12:02:25 +0100 | [diff] [blame] | 751 | 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] | 752 | { |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 753 | struct hostent *he; |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 754 | /* max IPv6 length, including brackets and terminating NULL */ |
| 755 | char tmpip[48]; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 756 | int port = get_host_port(sa); |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 757 | |
| 758 | /* check IPv6 with square brackets */ |
| 759 | if (str[0] == '[') { |
| 760 | size_t iplength = strlen(str); |
| 761 | |
| 762 | if (iplength < 4) { |
| 763 | /* minimal size is 4 when using brackets "[::]" */ |
| 764 | goto fail; |
| 765 | } |
| 766 | else if (iplength >= sizeof(tmpip)) { |
| 767 | /* IPv6 literal can not be larger than tmpip */ |
| 768 | goto fail; |
| 769 | } |
| 770 | else { |
| 771 | if (str[iplength - 1] != ']') { |
| 772 | /* if address started with bracket, it should end with bracket */ |
| 773 | goto fail; |
| 774 | } |
| 775 | else { |
| 776 | memcpy(tmpip, str + 1, iplength - 2); |
| 777 | tmpip[iplength - 2] = '\0'; |
| 778 | str = tmpip; |
| 779 | } |
| 780 | } |
| 781 | } |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 782 | |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 783 | /* Any IPv6 address */ |
| 784 | if (str[0] == ':' && str[1] == ':' && !str[2]) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 785 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 786 | sa->ss_family = AF_INET6; |
| 787 | else if (sa->ss_family != AF_INET6) |
| 788 | goto fail; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 789 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 790 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 791 | } |
| 792 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 793 | /* Any address for the family, defaults to IPv4 */ |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 794 | if (!str[0] || (str[0] == '*' && !str[1])) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 795 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 796 | sa->ss_family = AF_INET; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 797 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 798 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* check for IPv6 first */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 802 | if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET6) && |
| 803 | inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)sa)->sin6_addr)) { |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 804 | sa->ss_family = AF_INET6; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 805 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 806 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 807 | } |
| 808 | |
| 809 | /* then check for IPv4 */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 810 | if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET) && |
| 811 | inet_pton(AF_INET, str, &((struct sockaddr_in *)sa)->sin_addr)) { |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 812 | sa->ss_family = AF_INET; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 813 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 814 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 815 | } |
| 816 | |
Thierry FOURNIER | 58639a0 | 2014-11-25 12:02:25 +0100 | [diff] [blame] | 817 | if (!resolve) |
| 818 | return NULL; |
| 819 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 820 | if (!resolv_hostname_validation(str, NULL)) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 821 | return NULL; |
| 822 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 823 | #ifdef USE_GETADDRINFO |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 824 | if (global.tune.options & GTUNE_USE_GAI) { |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 825 | struct addrinfo hints, *result; |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 826 | int success = 0; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 827 | |
| 828 | memset(&result, 0, sizeof(result)); |
| 829 | memset(&hints, 0, sizeof(hints)); |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 830 | hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 831 | hints.ai_socktype = SOCK_DGRAM; |
Dmitry Sivachenko | eab7f39 | 2015-10-02 01:01:58 +0200 | [diff] [blame] | 832 | hints.ai_flags = 0; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 833 | hints.ai_protocol = 0; |
| 834 | |
| 835 | if (getaddrinfo(str, NULL, &hints, &result) == 0) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 836 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 837 | sa->ss_family = result->ai_family; |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 838 | else if (sa->ss_family != result->ai_family) { |
| 839 | freeaddrinfo(result); |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 840 | goto fail; |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 841 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 842 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 843 | switch (result->ai_family) { |
| 844 | case AF_INET: |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 845 | memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen); |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 846 | set_host_port(sa, port); |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 847 | success = 1; |
| 848 | break; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 849 | case AF_INET6: |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 850 | memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen); |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 851 | set_host_port(sa, port); |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 852 | success = 1; |
| 853 | break; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 854 | } |
| 855 | } |
| 856 | |
Sean Carey | 58ea039 | 2013-02-15 23:39:18 +0100 | [diff] [blame] | 857 | if (result) |
| 858 | freeaddrinfo(result); |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 859 | |
| 860 | if (success) |
| 861 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 862 | } |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 863 | #endif |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 864 | /* try to resolve an IPv4/IPv6 hostname */ |
| 865 | he = gethostbyname(str); |
| 866 | if (he) { |
| 867 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 868 | sa->ss_family = he->h_addrtype; |
| 869 | else if (sa->ss_family != he->h_addrtype) |
| 870 | goto fail; |
| 871 | |
| 872 | switch (sa->ss_family) { |
| 873 | case AF_INET: |
| 874 | ((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] | 875 | set_host_port(sa, port); |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 876 | return sa; |
| 877 | case AF_INET6: |
| 878 | ((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] | 879 | set_host_port(sa, port); |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 880 | return sa; |
| 881 | } |
| 882 | } |
| 883 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 884 | /* unsupported address family */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 885 | fail: |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 886 | return NULL; |
| 887 | } |
| 888 | |
| 889 | /* |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 890 | * Converts <str> to a locally allocated struct sockaddr_storage *, and a port |
| 891 | * range or offset consisting in two integers that the caller will have to |
| 892 | * check to find the relevant input format. The following format are supported : |
| 893 | * |
| 894 | * String format | address | port | low | high |
| 895 | * addr | <addr> | 0 | 0 | 0 |
| 896 | * addr: | <addr> | 0 | 0 | 0 |
| 897 | * addr:port | <addr> | <port> | <port> | <port> |
| 898 | * addr:pl-ph | <addr> | <pl> | <pl> | <ph> |
| 899 | * addr:+port | <addr> | <port> | 0 | <port> |
| 900 | * addr:-port | <addr> |-<port> | <port> | 0 |
| 901 | * |
| 902 | * The detection of a port range or increment by the caller is made by |
| 903 | * comparing <low> and <high>. If both are equal, then port 0 means no port |
| 904 | * was specified. The caller may pass NULL for <low> and <high> if it is not |
| 905 | * interested in retrieving port ranges. |
| 906 | * |
| 907 | * Note that <addr> above may also be : |
| 908 | * - empty ("") => family will be AF_INET and address will be INADDR_ANY |
| 909 | * - "*" => family will be AF_INET and address will be INADDR_ANY |
| 910 | * - "::" => family will be AF_INET6 and address will be IN6ADDR_ANY |
| 911 | * - a host name => family and address will depend on host name resolving. |
| 912 | * |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 913 | * A prefix may be passed in before the address above to force the family : |
| 914 | * - "ipv4@" => force address to resolve as IPv4 and fail if not possible. |
| 915 | * - "ipv6@" => force address to resolve as IPv6 and fail if not possible. |
| 916 | * - "unix@" => force address to be a path to a UNIX socket even if the |
| 917 | * path does not start with a '/' |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 918 | * - 'abns@' -> force address to belong to the abstract namespace (Linux |
| 919 | * only). These sockets are just like Unix sockets but without |
| 920 | * the need for an underlying file system. The address is a |
| 921 | * string. Technically it's like a Unix socket with a zero in |
| 922 | * the first byte of the address. |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 923 | * - "fd@" => an integer must follow, and is a file descriptor number. |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 924 | * |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 925 | * IPv6 addresses can be declared with or without square brackets. When using |
| 926 | * square brackets for IPv6 addresses, the port separator (colon) is optional. |
| 927 | * If not using square brackets, and in order to avoid any ambiguity with |
| 928 | * IPv6 addresses, the last colon ':' is mandatory even when no port is specified. |
| 929 | * NULL is returned if the address cannot be parsed. The <low> and <high> ports |
| 930 | * are always initialized if non-null, even for non-IP families. |
Willy Tarreau | d393a62 | 2013-03-04 18:22:00 +0100 | [diff] [blame] | 931 | * |
| 932 | * If <pfx> is non-null, it is used as a string prefix before any path-based |
| 933 | * address (typically the path to a unix socket). |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 934 | * |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 935 | * if <fqdn> is non-null, it will be filled with : |
| 936 | * - a pointer to the FQDN of the server name to resolve if there's one, and |
| 937 | * that the caller will have to free(), |
| 938 | * - NULL if there was an explicit address that doesn't require resolution. |
| 939 | * |
Willy Tarreau | cd3a5591 | 2020-09-04 15:30:46 +0200 | [diff] [blame] | 940 | * Hostnames are only resolved if <opts> has PA_O_RESOLVE. Otherwise <fqdn> is |
| 941 | * still honored so it is possible for the caller to know whether a resolution |
| 942 | * failed by clearing this flag and checking if <fqdn> was filled, indicating |
| 943 | * the need for a resolution. |
Thierry FOURNIER | 7fe3be7 | 2015-09-26 20:03:36 +0200 | [diff] [blame] | 944 | * |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 945 | * 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] | 946 | * the address when cast to sockaddr_in and the address family is |
| 947 | * AF_CUST_EXISTING_FD. |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 948 | * |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 949 | * The matching protocol will be set into <proto> if non-null. |
| 950 | * |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 951 | * Any known file descriptor is also assigned to <fd> if non-null, otherwise it |
| 952 | * is forced to -1. |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 953 | */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 954 | struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int *high, int *fd, |
| 955 | struct protocol **proto, char **err, |
| 956 | const char *pfx, char **fqdn, unsigned int opts) |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 957 | { |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 958 | static THREAD_LOCAL struct sockaddr_storage ss; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 959 | struct sockaddr_storage *ret = NULL; |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 960 | struct protocol *new_proto = NULL; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 961 | char *back, *str2; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 962 | char *port1, *port2; |
| 963 | int portl, porth, porta; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 964 | int abstract = 0; |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 965 | int new_fd = -1; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 966 | enum proto_type proto_type; |
| 967 | int ctrl_type; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 968 | |
| 969 | portl = porth = porta = 0; |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 970 | if (fqdn) |
| 971 | *fqdn = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 972 | |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 973 | str2 = back = env_expand(strdup(str)); |
Willy Tarreau | df350f1 | 2013-03-01 20:22:54 +0100 | [diff] [blame] | 974 | if (str2 == NULL) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 975 | memprintf(err, "out of memory in '%s'", __FUNCTION__); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 976 | goto out; |
Willy Tarreau | df350f1 | 2013-03-01 20:22:54 +0100 | [diff] [blame] | 977 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 978 | |
Willy Tarreau | 9f69f46 | 2015-09-08 16:01:25 +0200 | [diff] [blame] | 979 | if (!*str2) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 980 | memprintf(err, "'%s' resolves to an empty address (environment variable missing?)", str); |
Willy Tarreau | 9f69f46 | 2015-09-08 16:01:25 +0200 | [diff] [blame] | 981 | goto out; |
| 982 | } |
| 983 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 984 | memset(&ss, 0, sizeof(ss)); |
| 985 | |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 986 | /* prepare the default socket types */ |
Willy Tarreau | f23b1bc | 2021-03-23 18:36:37 +0100 | [diff] [blame] | 987 | if ((opts & (PA_O_STREAM|PA_O_DGRAM)) == PA_O_DGRAM || |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 988 | ((opts & (PA_O_STREAM|PA_O_DGRAM)) == (PA_O_DGRAM|PA_O_STREAM) && (opts & PA_O_DEFAULT_DGRAM))) { |
| 989 | proto_type = PROTO_TYPE_DGRAM; |
| 990 | ctrl_type = SOCK_DGRAM; |
| 991 | } else { |
| 992 | proto_type = PROTO_TYPE_STREAM; |
| 993 | ctrl_type = SOCK_STREAM; |
| 994 | } |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 995 | |
| 996 | if (strncmp(str2, "stream+", 7) == 0) { |
| 997 | str2 += 7; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 998 | proto_type = PROTO_TYPE_STREAM; |
| 999 | ctrl_type = SOCK_STREAM; |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1000 | } |
| 1001 | else if (strncmp(str2, "dgram+", 6) == 0) { |
| 1002 | str2 += 6; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1003 | proto_type = PROTO_TYPE_DGRAM; |
| 1004 | ctrl_type = SOCK_DGRAM; |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1005 | } |
Willy Tarreau | 40725a4 | 2023-01-16 13:55:27 +0100 | [diff] [blame] | 1006 | else if (strncmp(str2, "quic+", 5) == 0) { |
| 1007 | str2 += 5; |
| 1008 | proto_type = PROTO_TYPE_DGRAM; |
| 1009 | ctrl_type = SOCK_STREAM; |
| 1010 | } |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1011 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1012 | if (strncmp(str2, "unix@", 5) == 0) { |
| 1013 | str2 += 5; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 1014 | abstract = 0; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1015 | ss.ss_family = AF_UNIX; |
| 1016 | } |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1017 | else if (strncmp(str2, "uxdg@", 5) == 0) { |
| 1018 | str2 += 5; |
| 1019 | abstract = 0; |
| 1020 | ss.ss_family = AF_UNIX; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1021 | proto_type = PROTO_TYPE_DGRAM; |
| 1022 | ctrl_type = SOCK_DGRAM; |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1023 | } |
| 1024 | else if (strncmp(str2, "uxst@", 5) == 0) { |
| 1025 | str2 += 5; |
| 1026 | abstract = 0; |
| 1027 | ss.ss_family = AF_UNIX; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1028 | proto_type = PROTO_TYPE_STREAM; |
| 1029 | ctrl_type = SOCK_STREAM; |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1030 | } |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 1031 | else if (strncmp(str2, "abns@", 5) == 0) { |
| 1032 | str2 += 5; |
| 1033 | abstract = 1; |
| 1034 | ss.ss_family = AF_UNIX; |
| 1035 | } |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1036 | else if (strncmp(str2, "ip@", 3) == 0) { |
| 1037 | str2 += 3; |
| 1038 | ss.ss_family = AF_UNSPEC; |
| 1039 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1040 | else if (strncmp(str2, "ipv4@", 5) == 0) { |
| 1041 | str2 += 5; |
| 1042 | ss.ss_family = AF_INET; |
| 1043 | } |
| 1044 | else if (strncmp(str2, "ipv6@", 5) == 0) { |
| 1045 | str2 += 5; |
| 1046 | ss.ss_family = AF_INET6; |
| 1047 | } |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1048 | else if (strncmp(str2, "tcp4@", 5) == 0) { |
| 1049 | str2 += 5; |
| 1050 | ss.ss_family = AF_INET; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1051 | proto_type = PROTO_TYPE_STREAM; |
| 1052 | ctrl_type = SOCK_STREAM; |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1053 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1054 | else if (strncmp(str2, "udp4@", 5) == 0) { |
| 1055 | str2 += 5; |
| 1056 | ss.ss_family = AF_INET; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1057 | proto_type = PROTO_TYPE_DGRAM; |
| 1058 | ctrl_type = SOCK_DGRAM; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1059 | } |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1060 | else if (strncmp(str2, "tcp6@", 5) == 0) { |
| 1061 | str2 += 5; |
| 1062 | ss.ss_family = AF_INET6; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1063 | proto_type = PROTO_TYPE_STREAM; |
| 1064 | ctrl_type = SOCK_STREAM; |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1065 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1066 | else if (strncmp(str2, "udp6@", 5) == 0) { |
| 1067 | str2 += 5; |
| 1068 | ss.ss_family = AF_INET6; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1069 | proto_type = PROTO_TYPE_DGRAM; |
| 1070 | ctrl_type = SOCK_DGRAM; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1071 | } |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1072 | else if (strncmp(str2, "tcp@", 4) == 0) { |
| 1073 | str2 += 4; |
| 1074 | ss.ss_family = AF_UNSPEC; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1075 | proto_type = PROTO_TYPE_STREAM; |
| 1076 | ctrl_type = SOCK_STREAM; |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1077 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1078 | else if (strncmp(str2, "udp@", 4) == 0) { |
| 1079 | str2 += 4; |
| 1080 | ss.ss_family = AF_UNSPEC; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1081 | proto_type = PROTO_TYPE_DGRAM; |
| 1082 | ctrl_type = SOCK_DGRAM; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1083 | } |
Frédéric Lécaille | 10caf65 | 2020-11-23 11:36:57 +0100 | [diff] [blame] | 1084 | else if (strncmp(str2, "quic4@", 6) == 0) { |
| 1085 | str2 += 6; |
| 1086 | ss.ss_family = AF_INET; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1087 | proto_type = PROTO_TYPE_DGRAM; |
Frédéric Lécaille | 10caf65 | 2020-11-23 11:36:57 +0100 | [diff] [blame] | 1088 | ctrl_type = SOCK_STREAM; |
| 1089 | } |
| 1090 | else if (strncmp(str2, "quic6@", 6) == 0) { |
| 1091 | str2 += 6; |
| 1092 | ss.ss_family = AF_INET6; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1093 | proto_type = PROTO_TYPE_DGRAM; |
Frédéric Lécaille | 10caf65 | 2020-11-23 11:36:57 +0100 | [diff] [blame] | 1094 | ctrl_type = SOCK_STREAM; |
| 1095 | } |
Willy Tarreau | 5a7beed | 2020-09-04 16:54:05 +0200 | [diff] [blame] | 1096 | else if (strncmp(str2, "fd@", 3) == 0) { |
| 1097 | str2 += 3; |
| 1098 | ss.ss_family = AF_CUST_EXISTING_FD; |
| 1099 | } |
| 1100 | else if (strncmp(str2, "sockpair@", 9) == 0) { |
| 1101 | str2 += 9; |
| 1102 | ss.ss_family = AF_CUST_SOCKPAIR; |
| 1103 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1104 | else if (*str2 == '/') { |
| 1105 | ss.ss_family = AF_UNIX; |
| 1106 | } |
| 1107 | else |
| 1108 | ss.ss_family = AF_UNSPEC; |
| 1109 | |
Willy Tarreau | 5a7beed | 2020-09-04 16:54:05 +0200 | [diff] [blame] | 1110 | if (ss.ss_family == AF_CUST_SOCKPAIR) { |
Willy Tarreau | a215be2 | 2020-09-16 10:14:16 +0200 | [diff] [blame] | 1111 | struct sockaddr_storage ss2; |
| 1112 | socklen_t addr_len; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 1113 | char *endptr; |
| 1114 | |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1115 | new_fd = strtol(str2, &endptr, 10); |
| 1116 | if (!*str2 || new_fd < 0 || *endptr) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1117 | memprintf(err, "file descriptor '%s' is not a valid integer in '%s'", str2, str); |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 1118 | goto out; |
| 1119 | } |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1120 | |
Willy Tarreau | a215be2 | 2020-09-16 10:14:16 +0200 | [diff] [blame] | 1121 | /* just verify that it's a socket */ |
| 1122 | addr_len = sizeof(ss2); |
| 1123 | if (getsockname(new_fd, (struct sockaddr *)&ss2, &addr_len) == -1) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1124 | memprintf(err, "cannot use file descriptor '%d' : %s.", new_fd, strerror(errno)); |
Willy Tarreau | a215be2 | 2020-09-16 10:14:16 +0200 | [diff] [blame] | 1125 | goto out; |
| 1126 | } |
| 1127 | |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1128 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = new_fd; |
| 1129 | ((struct sockaddr_in *)&ss)->sin_port = 0; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 1130 | } |
Willy Tarreau | 5a7beed | 2020-09-04 16:54:05 +0200 | [diff] [blame] | 1131 | else if (ss.ss_family == AF_CUST_EXISTING_FD) { |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 1132 | char *endptr; |
| 1133 | |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1134 | new_fd = strtol(str2, &endptr, 10); |
| 1135 | if (!*str2 || new_fd < 0 || *endptr) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1136 | memprintf(err, "file descriptor '%s' is not a valid integer in '%s'", str2, str); |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 1137 | goto out; |
| 1138 | } |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1139 | |
Willy Tarreau | 6edc722 | 2020-09-15 17:41:56 +0200 | [diff] [blame] | 1140 | if (opts & PA_O_SOCKET_FD) { |
| 1141 | socklen_t addr_len; |
| 1142 | int type; |
| 1143 | |
| 1144 | addr_len = sizeof(ss); |
| 1145 | if (getsockname(new_fd, (struct sockaddr *)&ss, &addr_len) == -1) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1146 | memprintf(err, "cannot use file descriptor '%d' : %s.", new_fd, strerror(errno)); |
Willy Tarreau | 6edc722 | 2020-09-15 17:41:56 +0200 | [diff] [blame] | 1147 | goto out; |
| 1148 | } |
| 1149 | |
| 1150 | addr_len = sizeof(type); |
| 1151 | if (getsockopt(new_fd, SOL_SOCKET, SO_TYPE, &type, &addr_len) != 0 || |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1152 | (type == SOCK_STREAM) != (proto_type == PROTO_TYPE_STREAM)) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1153 | memprintf(err, "socket on file descriptor '%d' is of the wrong type.", new_fd); |
Willy Tarreau | 6edc722 | 2020-09-15 17:41:56 +0200 | [diff] [blame] | 1154 | goto out; |
| 1155 | } |
| 1156 | |
| 1157 | porta = portl = porth = get_host_port(&ss); |
| 1158 | } else if (opts & PA_O_RAW_FD) { |
| 1159 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = new_fd; |
| 1160 | ((struct sockaddr_in *)&ss)->sin_port = 0; |
| 1161 | } else { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1162 | memprintf(err, "a file descriptor is not acceptable here in '%s'", str); |
Willy Tarreau | 6edc722 | 2020-09-15 17:41:56 +0200 | [diff] [blame] | 1163 | goto out; |
| 1164 | } |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 1165 | } |
| 1166 | else if (ss.ss_family == AF_UNIX) { |
Willy Tarreau | 8daa920 | 2019-06-16 18:16:33 +0200 | [diff] [blame] | 1167 | struct sockaddr_un *un = (struct sockaddr_un *)&ss; |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1168 | int prefix_path_len; |
| 1169 | int max_path_len; |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 1170 | int adr_len; |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1171 | |
| 1172 | /* complete unix socket path name during startup or soft-restart is |
| 1173 | * <unix_bind_prefix><path>.<pid>.<bak|tmp> |
| 1174 | */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 1175 | prefix_path_len = (pfx && !abstract) ? strlen(pfx) : 0; |
Willy Tarreau | 8daa920 | 2019-06-16 18:16:33 +0200 | [diff] [blame] | 1176 | max_path_len = (sizeof(un->sun_path) - 1) - |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 1177 | (abstract ? 0 : prefix_path_len + 1 + 5 + 1 + 3); |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1178 | |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 1179 | adr_len = strlen(str2); |
| 1180 | if (adr_len > max_path_len) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1181 | memprintf(err, "socket path '%s' too long (max %d)", str, max_path_len); |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1182 | goto out; |
| 1183 | } |
| 1184 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 1185 | /* 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] | 1186 | memset(un->sun_path, 0, sizeof(un->sun_path)); |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 1187 | if (prefix_path_len) |
Willy Tarreau | 8daa920 | 2019-06-16 18:16:33 +0200 | [diff] [blame] | 1188 | memcpy(un->sun_path, pfx, prefix_path_len); |
| 1189 | 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] | 1190 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1191 | else { /* IPv4 and IPv6 */ |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1192 | char *end = str2 + strlen(str2); |
| 1193 | char *chr; |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 1194 | |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1195 | /* search for : or ] whatever comes first */ |
| 1196 | for (chr = end-1; chr > str2; chr--) { |
| 1197 | if (*chr == ']' || *chr == ':') |
| 1198 | break; |
| 1199 | } |
| 1200 | |
| 1201 | if (*chr == ':') { |
| 1202 | /* Found a colon before a closing-bracket, must be a port separator. |
| 1203 | * This guarantee backward compatibility. |
| 1204 | */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1205 | if (!(opts & PA_O_PORT_OK)) { |
| 1206 | memprintf(err, "port specification not permitted here in '%s'", str); |
| 1207 | goto out; |
| 1208 | } |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1209 | *chr++ = '\0'; |
| 1210 | port1 = chr; |
| 1211 | } |
| 1212 | else { |
| 1213 | /* Either no colon and no closing-bracket |
| 1214 | * or directly ending with a closing-bracket. |
| 1215 | * However, no port. |
| 1216 | */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1217 | if (opts & PA_O_PORT_MAND) { |
| 1218 | memprintf(err, "missing port specification in '%s'", str); |
| 1219 | goto out; |
| 1220 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1221 | port1 = ""; |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1222 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1223 | |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1224 | if (isdigit((unsigned char)*port1)) { /* single port or range */ |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1225 | port2 = strchr(port1, '-'); |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1226 | if (port2) { |
| 1227 | if (!(opts & PA_O_PORT_RANGE)) { |
| 1228 | memprintf(err, "port range not permitted here in '%s'", str); |
| 1229 | goto out; |
| 1230 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1231 | *port2++ = '\0'; |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1232 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1233 | else |
| 1234 | port2 = port1; |
| 1235 | portl = atoi(port1); |
| 1236 | porth = atoi(port2); |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1237 | |
| 1238 | if (portl < !!(opts & PA_O_PORT_MAND) || portl > 65535) { |
| 1239 | memprintf(err, "invalid port '%s'", port1); |
| 1240 | goto out; |
| 1241 | } |
| 1242 | |
| 1243 | if (porth < !!(opts & PA_O_PORT_MAND) || porth > 65535) { |
| 1244 | memprintf(err, "invalid port '%s'", port2); |
| 1245 | goto out; |
| 1246 | } |
| 1247 | |
| 1248 | if (portl > porth) { |
| 1249 | memprintf(err, "invalid port range '%d-%d'", portl, porth); |
| 1250 | goto out; |
| 1251 | } |
| 1252 | |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1253 | porta = portl; |
| 1254 | } |
| 1255 | else if (*port1 == '-') { /* negative offset */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1256 | if (!(opts & PA_O_PORT_OFS)) { |
| 1257 | memprintf(err, "port offset not permitted here in '%s'", str); |
| 1258 | goto out; |
| 1259 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1260 | portl = atoi(port1 + 1); |
| 1261 | porta = -portl; |
| 1262 | } |
| 1263 | else if (*port1 == '+') { /* positive offset */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1264 | if (!(opts & PA_O_PORT_OFS)) { |
| 1265 | memprintf(err, "port offset not permitted here in '%s'", str); |
| 1266 | goto out; |
| 1267 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1268 | porth = atoi(port1 + 1); |
| 1269 | porta = porth; |
| 1270 | } |
| 1271 | else if (*port1) { /* other any unexpected char */ |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1272 | memprintf(err, "invalid character '%c' in port number '%s' in '%s'", *port1, port1, str); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1273 | goto out; |
| 1274 | } |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1275 | else if (opts & PA_O_PORT_MAND) { |
| 1276 | memprintf(err, "missing port specification in '%s'", str); |
| 1277 | goto out; |
| 1278 | } |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1279 | |
| 1280 | /* first try to parse the IP without resolving. If it fails, it |
| 1281 | * tells us we need to keep a copy of the FQDN to resolve later |
| 1282 | * 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] | 1283 | * 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] | 1284 | */ |
| 1285 | if (str2ip2(str2, &ss, 0) == NULL) { |
Willy Tarreau | cd3a5591 | 2020-09-04 15:30:46 +0200 | [diff] [blame] | 1286 | if ((!(opts & PA_O_RESOLVE) && !fqdn) || |
| 1287 | ((opts & PA_O_RESOLVE) && str2ip2(str2, &ss, 1) == NULL)) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1288 | memprintf(err, "invalid address: '%s' in '%s'", str2, str); |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1289 | goto out; |
| 1290 | } |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 1291 | |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1292 | if (fqdn) { |
| 1293 | if (str2 != back) |
| 1294 | memmove(back, str2, strlen(str2) + 1); |
| 1295 | *fqdn = back; |
| 1296 | back = NULL; |
| 1297 | } |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 1298 | } |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1299 | set_host_port(&ss, porta); |
Willy Tarreau | e4c58c8 | 2013-03-06 15:28:17 +0100 | [diff] [blame] | 1300 | } |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 1301 | |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1302 | if (ctrl_type == SOCK_STREAM && !(opts & PA_O_STREAM)) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1303 | memprintf(err, "stream-type address not acceptable in '%s'", str); |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1304 | goto out; |
| 1305 | } |
| 1306 | else if (ctrl_type == SOCK_DGRAM && !(opts & PA_O_DGRAM)) { |
Thierry Fournier | 3e83b09 | 2023-05-23 17:58:03 +0200 | [diff] [blame] | 1307 | memprintf(err, "dgram-type address not acceptable in '%s'", str); |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1308 | goto out; |
| 1309 | } |
| 1310 | |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1311 | if (proto || (opts & PA_O_CONNECT)) { |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1312 | /* Note: if the caller asks for a proto, we must find one, |
Emeric Brun | 2675490 | 2021-04-07 14:26:44 +0200 | [diff] [blame] | 1313 | * except if we inherit from a raw FD (family == AF_CUST_EXISTING_FD) |
| 1314 | * orif we return with an fqdn that will resolve later, |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1315 | * in which case the address is not known yet (this is only |
| 1316 | * for servers actually). |
| 1317 | */ |
Willy Tarreau | b2ffc99 | 2020-09-16 21:37:31 +0200 | [diff] [blame] | 1318 | new_proto = protocol_lookup(ss.ss_family, |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1319 | proto_type, |
Willy Tarreau | af9609b | 2020-09-16 22:04:46 +0200 | [diff] [blame] | 1320 | ctrl_type == SOCK_DGRAM); |
Willy Tarreau | b2ffc99 | 2020-09-16 21:37:31 +0200 | [diff] [blame] | 1321 | |
Emeric Brun | 2675490 | 2021-04-07 14:26:44 +0200 | [diff] [blame] | 1322 | if (!new_proto && (!fqdn || !*fqdn) && (ss.ss_family != AF_CUST_EXISTING_FD)) { |
Willy Tarreau | 3d7b468 | 2022-05-20 17:32:35 +0200 | [diff] [blame] | 1323 | memprintf(err, "unsupported %s protocol for %s family %d address '%s'%s", |
Willy Tarreau | 2b049b8 | 2022-05-20 17:28:30 +0200 | [diff] [blame] | 1324 | (ctrl_type == SOCK_DGRAM) ? "datagram" : "stream", |
| 1325 | (proto_type == PROTO_TYPE_DGRAM) ? "datagram" : "stream", |
| 1326 | ss.ss_family, |
Willy Tarreau | 3d7b468 | 2022-05-20 17:32:35 +0200 | [diff] [blame] | 1327 | str, |
Willy Tarreau | 3d7b468 | 2022-05-20 17:32:35 +0200 | [diff] [blame] | 1328 | #ifndef USE_QUIC |
Tim Duesterhus | 147eeb2 | 2022-05-22 12:40:58 +0200 | [diff] [blame] | 1329 | (ctrl_type == SOCK_STREAM && proto_type == PROTO_TYPE_DGRAM) |
| 1330 | ? "; QUIC is not compiled in if this is what you were looking for." |
| 1331 | : "" |
Willy Tarreau | 3d7b468 | 2022-05-20 17:32:35 +0200 | [diff] [blame] | 1332 | #else |
| 1333 | "" |
| 1334 | #endif |
Tim Duesterhus | 147eeb2 | 2022-05-22 12:40:58 +0200 | [diff] [blame] | 1335 | ); |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1336 | goto out; |
| 1337 | } |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1338 | |
| 1339 | if ((opts & PA_O_CONNECT) && new_proto && !new_proto->connect) { |
| 1340 | memprintf(err, "connect() not supported for this protocol family %d used by address '%s'", ss.ss_family, str); |
| 1341 | goto out; |
| 1342 | } |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1343 | } |
| 1344 | |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1345 | ret = &ss; |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 1346 | out: |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 1347 | if (port) |
| 1348 | *port = porta; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 1349 | if (low) |
| 1350 | *low = portl; |
| 1351 | if (high) |
| 1352 | *high = porth; |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1353 | if (fd) |
| 1354 | *fd = new_fd; |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1355 | if (proto) |
| 1356 | *proto = new_proto; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1357 | free(back); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 1358 | return ret; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1359 | } |
| 1360 | |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 1361 | /* converts <addr> and <port> into a string representation of the address and port. This is sort |
| 1362 | * of an inverse of str2sa_range, with some restrictions. The supported families are AF_INET, |
| 1363 | * AF_INET6, AF_UNIX, and AF_CUST_SOCKPAIR. If the family is unsopported NULL is returned. |
| 1364 | * If map_ports is true, then the sign of the port is included in the output, to indicate it is |
| 1365 | * relative to the incoming port. AF_INET and AF_INET6 will be in the form "<addr>:<port>". |
| 1366 | * AF_UNIX will either be just the path (if using a pathname) or "abns@<path>" if it is abstract. |
| 1367 | * AF_CUST_SOCKPAIR will be of the form "sockpair@<fd>". |
| 1368 | * |
| 1369 | * The returned char* is allocated, and it is the responsibility of the caller to free it. |
| 1370 | */ |
| 1371 | char * sa2str(const struct sockaddr_storage *addr, int port, int map_ports) |
| 1372 | { |
| 1373 | char buffer[INET6_ADDRSTRLEN]; |
| 1374 | char *out = NULL; |
| 1375 | const void *ptr; |
| 1376 | const char *path; |
| 1377 | |
| 1378 | switch (addr->ss_family) { |
| 1379 | case AF_INET: |
| 1380 | ptr = &((struct sockaddr_in *)addr)->sin_addr; |
| 1381 | break; |
| 1382 | case AF_INET6: |
| 1383 | ptr = &((struct sockaddr_in6 *)addr)->sin6_addr; |
| 1384 | break; |
| 1385 | case AF_UNIX: |
| 1386 | path = ((struct sockaddr_un *)addr)->sun_path; |
| 1387 | if (path[0] == '\0') { |
| 1388 | const int max_length = sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path) - 1; |
| 1389 | return memprintf(&out, "abns@%.*s", max_length, path+1); |
| 1390 | } else { |
| 1391 | return strdup(path); |
| 1392 | } |
| 1393 | case AF_CUST_SOCKPAIR: |
| 1394 | return memprintf(&out, "sockpair@%d", ((struct sockaddr_in *)addr)->sin_addr.s_addr); |
| 1395 | default: |
| 1396 | return NULL; |
| 1397 | } |
Tim Duesterhus | 22535a5 | 2022-05-23 09:30:49 +0200 | [diff] [blame] | 1398 | if (inet_ntop(addr->ss_family, ptr, buffer, sizeof(buffer)) == NULL) { |
| 1399 | BUG_ON(errno == ENOSPC); |
| 1400 | return NULL; |
| 1401 | } |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 1402 | if (map_ports) |
| 1403 | return memprintf(&out, "%s:%+d", buffer, port); |
| 1404 | else |
| 1405 | return memprintf(&out, "%s:%d", buffer, port); |
| 1406 | } |
| 1407 | |
| 1408 | |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1409 | /* converts <str> to a struct in_addr containing a network mask. It can be |
| 1410 | * 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] | 1411 | * if the conversion succeeds otherwise zero. |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1412 | */ |
| 1413 | int str2mask(const char *str, struct in_addr *mask) |
| 1414 | { |
| 1415 | if (strchr(str, '.') != NULL) { /* dotted notation */ |
| 1416 | if (!inet_pton(AF_INET, str, mask)) |
| 1417 | return 0; |
| 1418 | } |
| 1419 | else { /* mask length */ |
| 1420 | char *err; |
| 1421 | unsigned long len = strtol(str, &err, 10); |
| 1422 | |
| 1423 | if (!*str || (err && *err) || (unsigned)len > 32) |
| 1424 | return 0; |
Tim Duesterhus | 8575f72 | 2018-01-25 16:24:48 +0100 | [diff] [blame] | 1425 | |
| 1426 | len2mask4(len, mask); |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1427 | } |
| 1428 | return 1; |
| 1429 | } |
| 1430 | |
Tim Duesterhus | 4718517 | 2018-01-25 16:24:49 +0100 | [diff] [blame] | 1431 | /* 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] | 1432 | * 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] | 1433 | * if the conversion succeeds otherwise zero. |
| 1434 | */ |
| 1435 | int str2mask6(const char *str, struct in6_addr *mask) |
| 1436 | { |
| 1437 | if (strchr(str, ':') != NULL) { /* quadruplet notation */ |
| 1438 | if (!inet_pton(AF_INET6, str, mask)) |
| 1439 | return 0; |
| 1440 | } |
| 1441 | else { /* mask length */ |
| 1442 | char *err; |
| 1443 | unsigned long len = strtol(str, &err, 10); |
| 1444 | |
| 1445 | if (!*str || (err && *err) || (unsigned)len > 128) |
| 1446 | return 0; |
| 1447 | |
| 1448 | len2mask6(len, mask); |
| 1449 | } |
| 1450 | return 1; |
| 1451 | } |
| 1452 | |
Thierry FOURNIER | b050463 | 2013-12-14 15:39:02 +0100 | [diff] [blame] | 1453 | /* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion |
| 1454 | * succeeds otherwise zero. |
| 1455 | */ |
| 1456 | int cidr2dotted(int cidr, struct in_addr *mask) { |
| 1457 | |
| 1458 | if (cidr < 0 || cidr > 32) |
| 1459 | return 0; |
| 1460 | |
| 1461 | mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0; |
| 1462 | return 1; |
| 1463 | } |
| 1464 | |
Thierry Fournier | 70473a5 | 2016-02-17 17:12:14 +0100 | [diff] [blame] | 1465 | /* Convert mask from bit length form to in_addr form. |
| 1466 | * This function never fails. |
| 1467 | */ |
| 1468 | void len2mask4(int len, struct in_addr *addr) |
| 1469 | { |
| 1470 | if (len >= 32) { |
| 1471 | addr->s_addr = 0xffffffff; |
| 1472 | return; |
| 1473 | } |
| 1474 | if (len <= 0) { |
| 1475 | addr->s_addr = 0x00000000; |
| 1476 | return; |
| 1477 | } |
| 1478 | addr->s_addr = 0xffffffff << (32 - len); |
| 1479 | addr->s_addr = htonl(addr->s_addr); |
| 1480 | } |
| 1481 | |
| 1482 | /* Convert mask from bit length form to in6_addr form. |
| 1483 | * This function never fails. |
| 1484 | */ |
| 1485 | void len2mask6(int len, struct in6_addr *addr) |
| 1486 | { |
| 1487 | len2mask4(len, (struct in_addr *)&addr->s6_addr[0]); /* msb */ |
| 1488 | len -= 32; |
| 1489 | len2mask4(len, (struct in_addr *)&addr->s6_addr[4]); |
| 1490 | len -= 32; |
| 1491 | len2mask4(len, (struct in_addr *)&addr->s6_addr[8]); |
| 1492 | len -= 32; |
| 1493 | len2mask4(len, (struct in_addr *)&addr->s6_addr[12]); /* lsb */ |
| 1494 | } |
| 1495 | |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1496 | /* |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 1497 | * converts <str> to two struct in_addr* which must be pre-allocated. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1498 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1499 | * is optional and either in the dotted or CIDR notation. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1500 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 1501 | */ |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 1502 | 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] | 1503 | { |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1504 | __label__ out_free, out_err; |
| 1505 | char *c, *s; |
| 1506 | int ret_val; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1507 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1508 | s = strdup(str); |
| 1509 | if (!s) |
| 1510 | return 0; |
| 1511 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1512 | memset(mask, 0, sizeof(*mask)); |
| 1513 | memset(addr, 0, sizeof(*addr)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1514 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1515 | if ((c = strrchr(s, '/')) != NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1516 | *c++ = '\0'; |
| 1517 | /* c points to the mask */ |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1518 | if (!str2mask(c, mask)) |
| 1519 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1520 | } |
| 1521 | else { |
Willy Tarreau | ebd6160 | 2006-12-30 11:54:15 +0100 | [diff] [blame] | 1522 | mask->s_addr = ~0U; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1523 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1524 | if (!inet_pton(AF_INET, s, addr)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1525 | struct hostent *he; |
| 1526 | |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 1527 | if (!resolve) |
| 1528 | goto out_err; |
| 1529 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1530 | if ((he = gethostbyname(s)) == NULL) { |
| 1531 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1532 | } |
| 1533 | else |
| 1534 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 1535 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1536 | |
| 1537 | ret_val = 1; |
| 1538 | out_free: |
| 1539 | free(s); |
| 1540 | return ret_val; |
| 1541 | out_err: |
| 1542 | ret_val = 0; |
| 1543 | goto out_free; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1544 | } |
| 1545 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1546 | |
| 1547 | /* |
Willy Tarreau | 6d20e28 | 2012-04-27 22:49:47 +0200 | [diff] [blame] | 1548 | * converts <str> to two struct in6_addr* which must be pre-allocated. |
| 1549 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1550 | * is an optional number of bits (128 being the default). |
Willy Tarreau | 6d20e28 | 2012-04-27 22:49:47 +0200 | [diff] [blame] | 1551 | * Returns 1 if OK, 0 if error. |
| 1552 | */ |
| 1553 | int str62net(const char *str, struct in6_addr *addr, unsigned char *mask) |
| 1554 | { |
| 1555 | char *c, *s; |
| 1556 | int ret_val = 0; |
| 1557 | char *err; |
| 1558 | unsigned long len = 128; |
| 1559 | |
| 1560 | s = strdup(str); |
| 1561 | if (!s) |
| 1562 | return 0; |
| 1563 | |
| 1564 | memset(mask, 0, sizeof(*mask)); |
| 1565 | memset(addr, 0, sizeof(*addr)); |
| 1566 | |
| 1567 | if ((c = strrchr(s, '/')) != NULL) { |
| 1568 | *c++ = '\0'; /* c points to the mask */ |
| 1569 | if (!*c) |
| 1570 | goto out_free; |
| 1571 | |
| 1572 | len = strtoul(c, &err, 10); |
| 1573 | if ((err && *err) || (unsigned)len > 128) |
| 1574 | goto out_free; |
| 1575 | } |
| 1576 | *mask = len; /* OK we have a valid mask in <len> */ |
| 1577 | |
| 1578 | if (!inet_pton(AF_INET6, s, addr)) |
| 1579 | goto out_free; |
| 1580 | |
| 1581 | ret_val = 1; |
| 1582 | out_free: |
| 1583 | free(s); |
| 1584 | return ret_val; |
| 1585 | } |
| 1586 | |
| 1587 | |
| 1588 | /* |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1589 | * Parse IPv4 address found in url. Return the number of bytes parsed. It |
| 1590 | * expects exactly 4 numbers between 0 and 255 delimited by dots, and returns |
| 1591 | * zero in case of mismatch. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1592 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1593 | int url2ipv4(const char *addr, struct in_addr *dst) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1594 | { |
| 1595 | int saw_digit, octets, ch; |
| 1596 | u_char tmp[4], *tp; |
| 1597 | const char *cp = addr; |
| 1598 | |
| 1599 | saw_digit = 0; |
| 1600 | octets = 0; |
| 1601 | *(tp = tmp) = 0; |
| 1602 | |
| 1603 | while (*addr) { |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1604 | unsigned char digit = (ch = *addr) - '0'; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1605 | if (digit > 9 && ch != '.') |
| 1606 | break; |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1607 | addr++; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1608 | if (digit <= 9) { |
| 1609 | u_int new = *tp * 10 + digit; |
| 1610 | if (new > 255) |
| 1611 | return 0; |
| 1612 | *tp = new; |
| 1613 | if (!saw_digit) { |
| 1614 | if (++octets > 4) |
| 1615 | return 0; |
| 1616 | saw_digit = 1; |
| 1617 | } |
| 1618 | } else if (ch == '.' && saw_digit) { |
| 1619 | if (octets == 4) |
| 1620 | return 0; |
| 1621 | *++tp = 0; |
| 1622 | saw_digit = 0; |
| 1623 | } else |
| 1624 | return 0; |
| 1625 | } |
| 1626 | |
| 1627 | if (octets < 4) |
| 1628 | return 0; |
| 1629 | |
| 1630 | memcpy(&dst->s_addr, tmp, 4); |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1631 | return addr - cp; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1632 | } |
| 1633 | |
| 1634 | /* |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1635 | * Resolve destination server from URL. Convert <str> to a sockaddr_storage. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1636 | * <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] | 1637 | * the hostname. Actually only http and https are supported. <out> can be NULL. |
| 1638 | * This function returns the consumed length. It is useful if you parse complete |
| 1639 | * url like http://host:port/path, because the consumed length corresponds to |
| 1640 | * the first character of the path. If the conversion fails, it returns -1. |
| 1641 | * |
| 1642 | * This function tries to resolve the DNS name if haproxy is in starting mode. |
| 1643 | * So, this function may be used during the configuration parsing. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1644 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1645 | 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] | 1646 | { |
| 1647 | const char *curr = url, *cp = url; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1648 | const char *end; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1649 | int ret, url_code = 0; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1650 | unsigned long long int http_code = 0; |
| 1651 | int default_port; |
| 1652 | struct hostent *he; |
| 1653 | char *p; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1654 | |
| 1655 | /* Firstly, try to find :// pattern */ |
| 1656 | while (curr < url+ulen && url_code != 0x3a2f2f) { |
| 1657 | url_code = ((url_code & 0xffff) << 8); |
| 1658 | url_code += (unsigned char)*curr++; |
| 1659 | } |
| 1660 | |
| 1661 | /* Secondly, if :// pattern is found, verify parsed stuff |
| 1662 | * before pattern is matching our http pattern. |
| 1663 | * If so parse ip address and port in uri. |
| 1664 | * |
| 1665 | * WARNING: Current code doesn't support dynamic async dns resolver. |
| 1666 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1667 | if (url_code != 0x3a2f2f) |
| 1668 | return -1; |
| 1669 | |
| 1670 | /* Copy scheme, and utrn to lower case. */ |
| 1671 | while (cp < curr - 3) |
| 1672 | http_code = (http_code << 8) + *cp++; |
| 1673 | http_code |= 0x2020202020202020ULL; /* Turn everything to lower case */ |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1674 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1675 | /* HTTP or HTTPS url matching */ |
| 1676 | if (http_code == 0x2020202068747470ULL) { |
| 1677 | default_port = 80; |
| 1678 | if (out) |
| 1679 | out->scheme = SCH_HTTP; |
| 1680 | } |
| 1681 | else if (http_code == 0x2020206874747073ULL) { |
| 1682 | default_port = 443; |
| 1683 | if (out) |
| 1684 | out->scheme = SCH_HTTPS; |
| 1685 | } |
| 1686 | else |
| 1687 | return -1; |
| 1688 | |
| 1689 | /* If the next char is '[', the host address is IPv6. */ |
| 1690 | if (*curr == '[') { |
| 1691 | curr++; |
| 1692 | |
| 1693 | /* Check trash size */ |
| 1694 | if (trash.size < ulen) |
| 1695 | return -1; |
| 1696 | |
| 1697 | /* Look for ']' and copy the address in a trash buffer. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1698 | p = trash.area; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1699 | for (end = curr; |
| 1700 | end < url + ulen && *end != ']'; |
| 1701 | end++, p++) |
| 1702 | *p = *end; |
| 1703 | if (*end != ']') |
| 1704 | return -1; |
| 1705 | *p = '\0'; |
| 1706 | |
| 1707 | /* Update out. */ |
| 1708 | if (out) { |
| 1709 | out->host = curr; |
| 1710 | out->host_len = end - curr; |
| 1711 | } |
| 1712 | |
| 1713 | /* Try IPv6 decoding. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1714 | 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] | 1715 | return -1; |
| 1716 | end++; |
| 1717 | |
| 1718 | /* Decode port. */ |
William Lallemand | 3d7a918 | 2022-03-25 17:37:51 +0100 | [diff] [blame] | 1719 | if (end < url + ulen && *end == ':') { |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1720 | end++; |
| 1721 | default_port = read_uint(&end, url + ulen); |
| 1722 | } |
| 1723 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port); |
| 1724 | ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; |
| 1725 | return end - url; |
| 1726 | } |
| 1727 | else { |
William Lallemand | 8a91374 | 2022-02-18 16:13:12 +0100 | [diff] [blame] | 1728 | /* we need to copy the string into the trash because url2ipv4 |
| 1729 | * needs a \0 at the end of the string */ |
| 1730 | if (trash.size < ulen) |
| 1731 | return -1; |
| 1732 | |
| 1733 | memcpy(trash.area, curr, ulen - (curr - url)); |
| 1734 | trash.area[ulen - (curr - url)] = '\0'; |
| 1735 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1736 | /* We are looking for IP address. If you want to parse and |
| 1737 | * resolve hostname found in url, you can use str2sa_range(), but |
| 1738 | * be warned this can slow down global daemon performances |
| 1739 | * while handling lagging dns responses. |
| 1740 | */ |
William Lallemand | 8a91374 | 2022-02-18 16:13:12 +0100 | [diff] [blame] | 1741 | ret = url2ipv4(trash.area, &((struct sockaddr_in *)addr)->sin_addr); |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1742 | if (ret) { |
| 1743 | /* Update out. */ |
| 1744 | if (out) { |
| 1745 | out->host = curr; |
| 1746 | out->host_len = ret; |
| 1747 | } |
| 1748 | |
William Lallemand | b938b77 | 2022-03-24 21:59:03 +0100 | [diff] [blame] | 1749 | curr += ret; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1750 | |
| 1751 | /* Decode port. */ |
William Lallemand | 3d7a918 | 2022-03-25 17:37:51 +0100 | [diff] [blame] | 1752 | if (curr < url + ulen && *curr == ':') { |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1753 | curr++; |
| 1754 | default_port = read_uint(&curr, url + ulen); |
| 1755 | } |
| 1756 | ((struct sockaddr_in *)addr)->sin_port = htons(default_port); |
| 1757 | |
| 1758 | /* Set family. */ |
| 1759 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 1760 | return curr - url; |
| 1761 | } |
| 1762 | else if (global.mode & MODE_STARTING) { |
| 1763 | /* The IPv4 and IPv6 decoding fails, maybe the url contain name. Try to execute |
| 1764 | * synchronous DNS request only if HAProxy is in the start state. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1765 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1766 | |
| 1767 | /* look for : or / or end */ |
| 1768 | for (end = curr; |
| 1769 | end < url + ulen && *end != '/' && *end != ':'; |
| 1770 | end++); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1771 | memcpy(trash.area, curr, end - curr); |
| 1772 | trash.area[end - curr] = '\0'; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1773 | |
| 1774 | /* try to resolve an IPv4/IPv6 hostname */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1775 | he = gethostbyname(trash.area); |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1776 | if (!he) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1777 | return -1; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1778 | |
| 1779 | /* Update out. */ |
| 1780 | if (out) { |
| 1781 | out->host = curr; |
| 1782 | out->host_len = end - curr; |
| 1783 | } |
| 1784 | |
| 1785 | /* Decode port. */ |
William Lallemand | 3d7a918 | 2022-03-25 17:37:51 +0100 | [diff] [blame] | 1786 | if (end < url + ulen && *end == ':') { |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1787 | end++; |
| 1788 | default_port = read_uint(&end, url + ulen); |
| 1789 | } |
| 1790 | |
| 1791 | /* Copy IP address, set port and family. */ |
| 1792 | switch (he->h_addrtype) { |
| 1793 | case AF_INET: |
| 1794 | ((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 1795 | ((struct sockaddr_in *)addr)->sin_port = htons(default_port); |
| 1796 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 1797 | return end - url; |
| 1798 | |
| 1799 | case AF_INET6: |
| 1800 | ((struct sockaddr_in6 *)addr)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list); |
| 1801 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port); |
| 1802 | ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; |
| 1803 | return end - url; |
| 1804 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1805 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1806 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1807 | return -1; |
| 1808 | } |
| 1809 | |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1810 | /* Tries to convert a sockaddr_storage address to text form. Upon success, the |
| 1811 | * address family is returned so that it's easy for the caller to adapt to the |
| 1812 | * output format. Zero is returned if the address family is not supported. -1 |
| 1813 | * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are |
| 1814 | * supported. |
| 1815 | */ |
Willy Tarreau | d5ec4bf | 2019-04-25 17:48:16 +0200 | [diff] [blame] | 1816 | 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] | 1817 | { |
| 1818 | |
Willy Tarreau | d5ec4bf | 2019-04-25 17:48:16 +0200 | [diff] [blame] | 1819 | const void *ptr; |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1820 | |
| 1821 | if (size < 5) |
| 1822 | return 0; |
| 1823 | *str = '\0'; |
| 1824 | |
| 1825 | switch (addr->ss_family) { |
| 1826 | case AF_INET: |
| 1827 | ptr = &((struct sockaddr_in *)addr)->sin_addr; |
| 1828 | break; |
| 1829 | case AF_INET6: |
| 1830 | ptr = &((struct sockaddr_in6 *)addr)->sin6_addr; |
| 1831 | break; |
| 1832 | case AF_UNIX: |
| 1833 | memcpy(str, "unix", 5); |
| 1834 | return addr->ss_family; |
| 1835 | default: |
| 1836 | return 0; |
| 1837 | } |
| 1838 | |
| 1839 | if (inet_ntop(addr->ss_family, ptr, str, size)) |
| 1840 | return addr->ss_family; |
| 1841 | |
| 1842 | /* failed */ |
| 1843 | return -1; |
| 1844 | } |
| 1845 | |
Simon Horman | 75ab8bd | 2014-06-16 09:39:41 +0900 | [diff] [blame] | 1846 | /* Tries to convert a sockaddr_storage port to text form. Upon success, the |
| 1847 | * address family is returned so that it's easy for the caller to adapt to the |
| 1848 | * output format. Zero is returned if the address family is not supported. -1 |
| 1849 | * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are |
| 1850 | * supported. |
| 1851 | */ |
Willy Tarreau | d5ec4bf | 2019-04-25 17:48:16 +0200 | [diff] [blame] | 1852 | 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] | 1853 | { |
| 1854 | |
| 1855 | uint16_t port; |
| 1856 | |
| 1857 | |
Willy Tarreau | d7dad1b | 2017-01-06 16:46:22 +0100 | [diff] [blame] | 1858 | if (size < 6) |
Simon Horman | 75ab8bd | 2014-06-16 09:39:41 +0900 | [diff] [blame] | 1859 | return 0; |
| 1860 | *str = '\0'; |
| 1861 | |
| 1862 | switch (addr->ss_family) { |
| 1863 | case AF_INET: |
| 1864 | port = ((struct sockaddr_in *)addr)->sin_port; |
| 1865 | break; |
| 1866 | case AF_INET6: |
| 1867 | port = ((struct sockaddr_in6 *)addr)->sin6_port; |
| 1868 | break; |
| 1869 | case AF_UNIX: |
| 1870 | memcpy(str, "unix", 5); |
| 1871 | return addr->ss_family; |
| 1872 | default: |
| 1873 | return 0; |
| 1874 | } |
| 1875 | |
| 1876 | snprintf(str, size, "%u", ntohs(port)); |
| 1877 | return addr->ss_family; |
| 1878 | } |
| 1879 | |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 1880 | /* check if the given address is local to the system or not. It will return |
| 1881 | * -1 when it's not possible to know, 0 when the address is not local, 1 when |
| 1882 | * it is. We don't want to iterate over all interfaces for this (and it is not |
| 1883 | * portable). So instead we try to bind in UDP to this address on a free non |
| 1884 | * privileged port and to connect to the same address, port 0 (connect doesn't |
| 1885 | * care). If it succeeds, we own the address. Note that non-inet addresses are |
| 1886 | * considered local since they're most likely AF_UNIX. |
| 1887 | */ |
| 1888 | int addr_is_local(const struct netns_entry *ns, |
| 1889 | const struct sockaddr_storage *orig) |
| 1890 | { |
| 1891 | struct sockaddr_storage addr; |
| 1892 | int result; |
| 1893 | int fd; |
| 1894 | |
| 1895 | if (!is_inet_addr(orig)) |
| 1896 | return 1; |
| 1897 | |
| 1898 | memcpy(&addr, orig, sizeof(addr)); |
| 1899 | set_host_port(&addr, 0); |
| 1900 | |
| 1901 | fd = my_socketat(ns, addr.ss_family, SOCK_DGRAM, IPPROTO_UDP); |
| 1902 | if (fd < 0) |
| 1903 | return -1; |
| 1904 | |
| 1905 | result = -1; |
| 1906 | if (bind(fd, (struct sockaddr *)&addr, get_addr_len(&addr)) == 0) { |
| 1907 | if (connect(fd, (struct sockaddr *)&addr, get_addr_len(&addr)) == -1) |
| 1908 | result = 0; // fail, non-local address |
| 1909 | else |
| 1910 | result = 1; // success, local address |
| 1911 | } |
| 1912 | else { |
| 1913 | if (errno == EADDRNOTAVAIL) |
| 1914 | result = 0; // definitely not local :-) |
| 1915 | } |
| 1916 | close(fd); |
| 1917 | |
| 1918 | return result; |
| 1919 | } |
| 1920 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1921 | /* will try to encode the string <string> replacing all characters tagged in |
| 1922 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 1923 | * prefixed by <escape>, and will store the result between <start> (included) |
| 1924 | * and <stop> (excluded), and will always terminate the string with a '\0' |
Aurelien DARRAGON | ebcab99 | 2024-04-09 11:44:54 +0200 | [diff] [blame] | 1925 | * before <stop>. If bytes are missing between <start> and <stop>, then the |
| 1926 | * conversion will be incomplete and truncated. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1927 | * The input string must also be zero-terminated. |
Aurelien DARRAGON | ebcab99 | 2024-04-09 11:44:54 +0200 | [diff] [blame] | 1928 | * |
| 1929 | * Return the address of the \0 character, or NULL on error |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1930 | */ |
| 1931 | const char hextab[16] = "0123456789ABCDEF"; |
| 1932 | char *encode_string(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1933 | const char escape, const long *map, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1934 | const char *string) |
| 1935 | { |
| 1936 | if (start < stop) { |
| 1937 | stop--; /* reserve one byte for the final '\0' */ |
| 1938 | while (start < stop && *string != '\0') { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1939 | if (!ha_bit_test((unsigned char)(*string), map)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1940 | *start++ = *string; |
| 1941 | else { |
| 1942 | if (start + 3 >= stop) |
| 1943 | break; |
| 1944 | *start++ = escape; |
| 1945 | *start++ = hextab[(*string >> 4) & 15]; |
| 1946 | *start++ = hextab[*string & 15]; |
| 1947 | } |
| 1948 | string++; |
| 1949 | } |
| 1950 | *start = '\0'; |
Aurelien DARRAGON | ebcab99 | 2024-04-09 11:44:54 +0200 | [diff] [blame] | 1951 | return start; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1952 | } |
Aurelien DARRAGON | ebcab99 | 2024-04-09 11:44:54 +0200 | [diff] [blame] | 1953 | return NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1954 | } |
| 1955 | |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1956 | /* |
| 1957 | * Same behavior as encode_string() above, except that it encodes chunk |
| 1958 | * <chunk> instead of a string. |
| 1959 | */ |
| 1960 | char *encode_chunk(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1961 | const char escape, const long *map, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1962 | const struct buffer *chunk) |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1963 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1964 | char *str = chunk->area; |
| 1965 | char *end = chunk->area + chunk->data; |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1966 | |
| 1967 | if (start < stop) { |
| 1968 | stop--; /* reserve one byte for the final '\0' */ |
| 1969 | while (start < stop && str < end) { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1970 | if (!ha_bit_test((unsigned char)(*str), map)) |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1971 | *start++ = *str; |
| 1972 | else { |
| 1973 | if (start + 3 >= stop) |
| 1974 | break; |
| 1975 | *start++ = escape; |
| 1976 | *start++ = hextab[(*str >> 4) & 15]; |
| 1977 | *start++ = hextab[*str & 15]; |
| 1978 | } |
| 1979 | str++; |
| 1980 | } |
| 1981 | *start = '\0'; |
Aurelien DARRAGON | ebcab99 | 2024-04-09 11:44:54 +0200 | [diff] [blame] | 1982 | return start; |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1983 | } |
Aurelien DARRAGON | ebcab99 | 2024-04-09 11:44:54 +0200 | [diff] [blame] | 1984 | return NULL; |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1985 | } |
| 1986 | |
Dragan Dosen | 0edd109 | 2016-02-12 13:23:02 +0100 | [diff] [blame] | 1987 | /* |
| 1988 | * Tries to prefix characters tagged in the <map> with the <escape> |
Aurelien DARRAGON | c5bff8e | 2022-09-20 14:33:18 +0200 | [diff] [blame] | 1989 | * character. The input <string> is processed until string_stop |
| 1990 | * is reached or NULL-byte is encountered. The result will |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 1991 | * be stored between <start> (included) and <stop> (excluded). This |
| 1992 | * function will always try to terminate the resulting string with a '\0' |
Aurelien DARRAGON | ebcab99 | 2024-04-09 11:44:54 +0200 | [diff] [blame] | 1993 | * before <stop>. |
| 1994 | * |
| 1995 | * Return the address of the \0 character, or NULL on error |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 1996 | */ |
| 1997 | char *escape_string(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1998 | const char escape, const long *map, |
Aurelien DARRAGON | c5bff8e | 2022-09-20 14:33:18 +0200 | [diff] [blame] | 1999 | const char *string, const char *string_stop) |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 2000 | { |
| 2001 | if (start < stop) { |
| 2002 | stop--; /* reserve one byte for the final '\0' */ |
Aurelien DARRAGON | c5bff8e | 2022-09-20 14:33:18 +0200 | [diff] [blame] | 2003 | while (start < stop && string < string_stop && *string != '\0') { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 2004 | if (!ha_bit_test((unsigned char)(*string), map)) |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 2005 | *start++ = *string; |
| 2006 | else { |
| 2007 | if (start + 2 >= stop) |
| 2008 | break; |
| 2009 | *start++ = escape; |
| 2010 | *start++ = *string; |
| 2011 | } |
| 2012 | string++; |
| 2013 | } |
| 2014 | *start = '\0'; |
Aurelien DARRAGON | ebcab99 | 2024-04-09 11:44:54 +0200 | [diff] [blame] | 2015 | return start; |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 2016 | } |
Aurelien DARRAGON | ebcab99 | 2024-04-09 11:44:54 +0200 | [diff] [blame] | 2017 | return NULL; |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 2018 | } |
| 2019 | |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2020 | /* Check a string for using it in a CSV output format. If the string contains |
| 2021 | * one of the following four char <">, <,>, CR or LF, the string is |
| 2022 | * encapsulated between <"> and the <"> are escaped by a <""> sequence. |
| 2023 | * <str> is the input string to be escaped. The function assumes that |
| 2024 | * the input string is null-terminated. |
| 2025 | * |
| 2026 | * 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] | 2027 | * 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] | 2028 | * format. |
| 2029 | * |
Emeric Brun | 36e5427 | 2024-01-23 15:44:32 +0100 | [diff] [blame] | 2030 | * printf("..., \"%s\", ...\r\n", csv_enc(str, 0, 0, &trash)); |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2031 | * |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 2032 | * If <quote> is 1, the converter puts the quotes only if any reserved character |
| 2033 | * is present. If <quote> is 2, the converter always puts the quotes. |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2034 | * |
Emeric Brun | 36e5427 | 2024-01-23 15:44:32 +0100 | [diff] [blame] | 2035 | * If <oneline> is not 0, CRs are skipped and LFs are replaced by spaces. |
| 2036 | * This re-format multi-lines strings to only one line. The purpose is to |
| 2037 | * allow a line by line parsing but also to keep the output compliant with |
| 2038 | * the CLI witch uses LF to defines the end of the response. |
| 2039 | * |
| 2040 | * If <oneline> is 2, In addition to previous action, the trailing spaces are |
| 2041 | * removed. |
| 2042 | * |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2043 | * <output> is a struct buffer used for storing the output string. |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2044 | * |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2045 | * The function returns the converted string on its output. If an error |
| 2046 | * 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] | 2047 | * for using the function directly as printf() argument. |
| 2048 | * |
| 2049 | * If the output buffer is too short to contain the input string, the result |
| 2050 | * is truncated. |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2051 | * |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 2052 | * This function appends the encoding to the existing output chunk, and it |
| 2053 | * guarantees that it starts immediately at the first available character of |
| 2054 | * the chunk. Please use csv_enc() instead if you want to replace the output |
| 2055 | * chunk. |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2056 | */ |
Emeric Brun | 36e5427 | 2024-01-23 15:44:32 +0100 | [diff] [blame] | 2057 | const char *csv_enc_append(const char *str, int quote, int oneline, struct buffer *output) |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2058 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2059 | char *end = output->area + output->size; |
| 2060 | char *out = output->area + output->data; |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2061 | char *ptr = out; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2062 | |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 2063 | if (quote == 1) { |
| 2064 | /* automatic quoting: first verify if we'll have to quote the string */ |
| 2065 | if (!strpbrk(str, "\n\r,\"")) |
| 2066 | quote = 0; |
| 2067 | } |
| 2068 | |
| 2069 | if (quote) |
| 2070 | *ptr++ = '"'; |
| 2071 | |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2072 | while (*str && ptr < end - 2) { /* -2 for reserving space for <"> and \0. */ |
Emeric Brun | 36e5427 | 2024-01-23 15:44:32 +0100 | [diff] [blame] | 2073 | if (oneline) { |
| 2074 | if (*str == '\n' ) { |
| 2075 | /* replace LF by a space */ |
| 2076 | *ptr++ = ' '; |
| 2077 | str++; |
| 2078 | continue; |
| 2079 | } |
| 2080 | else if (*str == '\r' ) { |
| 2081 | /* skip CR */ |
| 2082 | str++; |
| 2083 | continue; |
| 2084 | } |
| 2085 | } |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2086 | *ptr = *str; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2087 | if (*str == '"') { |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2088 | ptr++; |
| 2089 | if (ptr >= end - 2) { |
| 2090 | ptr--; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2091 | break; |
| 2092 | } |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2093 | *ptr = '"'; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2094 | } |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2095 | ptr++; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2096 | str++; |
| 2097 | } |
| 2098 | |
Emeric Brun | 36e5427 | 2024-01-23 15:44:32 +0100 | [diff] [blame] | 2099 | if (oneline == 2) { |
| 2100 | /* remove trailing spaces */ |
| 2101 | while (ptr > out && *(ptr - 1) == ' ') |
| 2102 | ptr--; |
| 2103 | } |
| 2104 | |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 2105 | if (quote) |
| 2106 | *ptr++ = '"'; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2107 | |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2108 | *ptr = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2109 | output->data = ptr - output->area; |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2110 | return out; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2111 | } |
| 2112 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2113 | /* Decode an URL-encoded string in-place. The resulting string might |
| 2114 | * be shorter. If some forbidden characters are found, the conversion is |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 2115 | * aborted, the string is truncated before the issue and a negative value is |
| 2116 | * returned, otherwise the operation returns the length of the decoded string. |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 2117 | * If the 'in_form' argument is non-nul the string is assumed to be part of |
| 2118 | * an "application/x-www-form-urlencoded" encoded string, and the '+' will be |
| 2119 | * turned to a space. If it's zero, this will only be done after a question |
| 2120 | * mark ('?'). |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2121 | */ |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 2122 | int url_decode(char *string, int in_form) |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2123 | { |
| 2124 | char *in, *out; |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 2125 | int ret = -1; |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2126 | |
| 2127 | in = string; |
| 2128 | out = string; |
| 2129 | while (*in) { |
| 2130 | switch (*in) { |
| 2131 | case '+' : |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 2132 | *out++ = in_form ? ' ' : *in; |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2133 | break; |
| 2134 | case '%' : |
| 2135 | if (!ishex(in[1]) || !ishex(in[2])) |
| 2136 | goto end; |
| 2137 | *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]); |
| 2138 | in += 2; |
| 2139 | break; |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 2140 | case '?': |
| 2141 | in_form = 1; |
Willy Tarreau | 7de8de0 | 2022-11-14 07:20:09 +0100 | [diff] [blame] | 2142 | __fallthrough; |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2143 | default: |
| 2144 | *out++ = *in; |
| 2145 | break; |
| 2146 | } |
| 2147 | in++; |
| 2148 | } |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 2149 | ret = out - string; /* success */ |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2150 | end: |
| 2151 | *out = 0; |
| 2152 | return ret; |
| 2153 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2154 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2155 | unsigned int str2ui(const char *s) |
| 2156 | { |
| 2157 | return __str2ui(s); |
| 2158 | } |
| 2159 | |
| 2160 | unsigned int str2uic(const char *s) |
| 2161 | { |
| 2162 | return __str2uic(s); |
| 2163 | } |
| 2164 | |
| 2165 | unsigned int strl2ui(const char *s, int len) |
| 2166 | { |
| 2167 | return __strl2ui(s, len); |
| 2168 | } |
| 2169 | |
| 2170 | unsigned int strl2uic(const char *s, int len) |
| 2171 | { |
| 2172 | return __strl2uic(s, len); |
| 2173 | } |
| 2174 | |
Willy Tarreau | 4ec83cd | 2010-10-15 23:19:55 +0200 | [diff] [blame] | 2175 | unsigned int read_uint(const char **s, const char *end) |
| 2176 | { |
| 2177 | return __read_uint(s, end); |
| 2178 | } |
| 2179 | |
Thierry FOURNIER | 763a5d8 | 2015-07-06 23:09:52 +0200 | [diff] [blame] | 2180 | /* This function reads an unsigned integer from the string pointed to by <s> and |
| 2181 | * returns it. The <s> pointer is adjusted to point to the first unread char. The |
| 2182 | * function automatically stops at <end>. If the number overflows, the 2^64-1 |
| 2183 | * value is returned. |
| 2184 | */ |
| 2185 | unsigned long long int read_uint64(const char **s, const char *end) |
| 2186 | { |
| 2187 | const char *ptr = *s; |
| 2188 | unsigned long long int i = 0, tmp; |
| 2189 | unsigned int j; |
| 2190 | |
| 2191 | while (ptr < end) { |
| 2192 | |
| 2193 | /* read next char */ |
| 2194 | j = *ptr - '0'; |
| 2195 | if (j > 9) |
| 2196 | goto read_uint64_end; |
| 2197 | |
| 2198 | /* add char to the number and check overflow. */ |
| 2199 | tmp = i * 10; |
| 2200 | if (tmp / 10 != i) { |
| 2201 | i = ULLONG_MAX; |
| 2202 | goto read_uint64_eat; |
| 2203 | } |
| 2204 | if (ULLONG_MAX - tmp < j) { |
| 2205 | i = ULLONG_MAX; |
| 2206 | goto read_uint64_eat; |
| 2207 | } |
| 2208 | i = tmp + j; |
| 2209 | ptr++; |
| 2210 | } |
| 2211 | read_uint64_eat: |
| 2212 | /* eat each numeric char */ |
| 2213 | while (ptr < end) { |
| 2214 | if ((unsigned int)(*ptr - '0') > 9) |
| 2215 | break; |
| 2216 | ptr++; |
| 2217 | } |
| 2218 | read_uint64_end: |
| 2219 | *s = ptr; |
| 2220 | return i; |
| 2221 | } |
| 2222 | |
| 2223 | /* This function reads an integer from the string pointed to by <s> and returns |
| 2224 | * it. The <s> pointer is adjusted to point to the first unread char. The function |
| 2225 | * automatically stops at <end>. Il the number is bigger than 2^63-2, the 2^63-1 |
| 2226 | * value is returned. If the number is lowest than -2^63-1, the -2^63 value is |
| 2227 | * returned. |
| 2228 | */ |
| 2229 | long long int read_int64(const char **s, const char *end) |
| 2230 | { |
| 2231 | unsigned long long int i = 0; |
| 2232 | int neg = 0; |
| 2233 | |
| 2234 | /* Look for minus char. */ |
| 2235 | if (**s == '-') { |
| 2236 | neg = 1; |
| 2237 | (*s)++; |
| 2238 | } |
| 2239 | else if (**s == '+') |
| 2240 | (*s)++; |
| 2241 | |
| 2242 | /* convert as positive number. */ |
| 2243 | i = read_uint64(s, end); |
| 2244 | |
| 2245 | if (neg) { |
| 2246 | if (i > 0x8000000000000000ULL) |
| 2247 | return LLONG_MIN; |
| 2248 | return -i; |
| 2249 | } |
| 2250 | if (i > 0x7fffffffffffffffULL) |
| 2251 | return LLONG_MAX; |
| 2252 | return i; |
| 2253 | } |
| 2254 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2255 | /* This one is 7 times faster than strtol() on athlon with checks. |
| 2256 | * It returns the value of the number composed of all valid digits read, |
| 2257 | * and can process negative numbers too. |
| 2258 | */ |
| 2259 | int strl2ic(const char *s, int len) |
| 2260 | { |
| 2261 | int i = 0; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2262 | int j, k; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2263 | |
| 2264 | if (len > 0) { |
| 2265 | if (*s != '-') { |
| 2266 | /* positive number */ |
| 2267 | while (len-- > 0) { |
| 2268 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2269 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2270 | if (j > 9) |
| 2271 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2272 | i = k + j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2273 | } |
| 2274 | } else { |
| 2275 | /* negative number */ |
| 2276 | s++; |
| 2277 | while (--len > 0) { |
| 2278 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2279 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2280 | if (j > 9) |
| 2281 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2282 | i = k - j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2283 | } |
| 2284 | } |
| 2285 | } |
| 2286 | return i; |
| 2287 | } |
| 2288 | |
| 2289 | |
| 2290 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 2291 | * signed integer which it stores into <ret>. It accurately detects any error |
| 2292 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 2293 | * applications designed for hostile environments. It returns zero when the |
| 2294 | * number has successfully been converted, non-zero otherwise. When an error |
| 2295 | * is returned, the <ret> value is left untouched. It is yet 5 to 40 times |
| 2296 | * faster than strtol(). |
| 2297 | */ |
| 2298 | int strl2irc(const char *s, int len, int *ret) |
| 2299 | { |
| 2300 | int i = 0; |
| 2301 | int j; |
| 2302 | |
| 2303 | if (!len) |
| 2304 | return 1; |
| 2305 | |
| 2306 | if (*s != '-') { |
| 2307 | /* positive number */ |
| 2308 | while (len-- > 0) { |
| 2309 | j = (*s++) - '0'; |
| 2310 | if (j > 9) return 1; /* invalid char */ |
| 2311 | if (i > INT_MAX / 10) return 1; /* check for multiply overflow */ |
| 2312 | i = i * 10; |
| 2313 | if (i + j < i) return 1; /* check for addition overflow */ |
| 2314 | i = i + j; |
| 2315 | } |
| 2316 | } else { |
| 2317 | /* negative number */ |
| 2318 | s++; |
| 2319 | while (--len > 0) { |
| 2320 | j = (*s++) - '0'; |
| 2321 | if (j > 9) return 1; /* invalid char */ |
| 2322 | if (i < INT_MIN / 10) return 1; /* check for multiply overflow */ |
| 2323 | i = i * 10; |
| 2324 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 2325 | i = i - j; |
| 2326 | } |
| 2327 | } |
| 2328 | *ret = i; |
| 2329 | return 0; |
| 2330 | } |
| 2331 | |
| 2332 | |
| 2333 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 2334 | * signed integer which it stores into <ret>. It accurately detects any error |
| 2335 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 2336 | * applications designed for hostile environments. It returns zero when the |
| 2337 | * number has successfully been converted, non-zero otherwise. When an error |
| 2338 | * 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] | 2339 | * than strl2irc(). |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2340 | */ |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2341 | |
| 2342 | int strl2llrc(const char *s, int len, long long *ret) |
| 2343 | { |
| 2344 | long long i = 0; |
| 2345 | int j; |
| 2346 | |
| 2347 | if (!len) |
| 2348 | return 1; |
| 2349 | |
| 2350 | if (*s != '-') { |
| 2351 | /* positive number */ |
| 2352 | while (len-- > 0) { |
| 2353 | j = (*s++) - '0'; |
| 2354 | if (j > 9) return 1; /* invalid char */ |
| 2355 | if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */ |
| 2356 | i = i * 10LL; |
| 2357 | if (i + j < i) return 1; /* check for addition overflow */ |
| 2358 | i = i + j; |
| 2359 | } |
| 2360 | } else { |
| 2361 | /* negative number */ |
| 2362 | s++; |
| 2363 | while (--len > 0) { |
| 2364 | j = (*s++) - '0'; |
| 2365 | if (j > 9) return 1; /* invalid char */ |
| 2366 | if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */ |
| 2367 | i = i * 10LL; |
| 2368 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 2369 | i = i - j; |
| 2370 | } |
| 2371 | } |
| 2372 | *ret = i; |
| 2373 | return 0; |
| 2374 | } |
| 2375 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 2376 | /* This function is used with pat_parse_dotted_ver(). It converts a string |
| 2377 | * composed by two number separated by a dot. Each part must contain in 16 bits |
| 2378 | * because internally they will be represented as a 32-bit quantity stored in |
| 2379 | * a 64-bit integer. It returns zero when the number has successfully been |
| 2380 | * converted, non-zero otherwise. When an error is returned, the <ret> value |
| 2381 | * is left untouched. |
| 2382 | * |
| 2383 | * "1.3" -> 0x0000000000010003 |
| 2384 | * "65535.65535" -> 0x00000000ffffffff |
| 2385 | */ |
| 2386 | int strl2llrc_dotted(const char *text, int len, long long *ret) |
| 2387 | { |
| 2388 | const char *end = &text[len]; |
| 2389 | const char *p; |
| 2390 | long long major, minor; |
| 2391 | |
| 2392 | /* Look for dot. */ |
| 2393 | for (p = text; p < end; p++) |
| 2394 | if (*p == '.') |
| 2395 | break; |
| 2396 | |
| 2397 | /* Convert major. */ |
| 2398 | if (strl2llrc(text, p - text, &major) != 0) |
| 2399 | return 1; |
| 2400 | |
| 2401 | /* Check major. */ |
| 2402 | if (major >= 65536) |
| 2403 | return 1; |
| 2404 | |
| 2405 | /* Convert minor. */ |
| 2406 | minor = 0; |
| 2407 | if (p < end) |
| 2408 | if (strl2llrc(p + 1, end - (p + 1), &minor) != 0) |
| 2409 | return 1; |
| 2410 | |
| 2411 | /* Check minor. */ |
| 2412 | if (minor >= 65536) |
| 2413 | return 1; |
| 2414 | |
| 2415 | /* Compose value. */ |
| 2416 | *ret = (major << 16) | (minor & 0xffff); |
| 2417 | return 0; |
| 2418 | } |
| 2419 | |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2420 | /* This function parses a time value optionally followed by a unit suffix among |
| 2421 | * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit |
| 2422 | * expected by the caller. The computation does its best to avoid overflows. |
| 2423 | * The value is returned in <ret> if everything is fine, and a NULL is returned |
| 2424 | * by the function. In case of error, a pointer to the error is returned and |
| 2425 | * <ret> is left untouched. Values are automatically rounded up when needed. |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2426 | * Values resulting in values larger than or equal to 2^31 after conversion are |
| 2427 | * reported as an overflow as value PARSE_TIME_OVER. Non-null values resulting |
| 2428 | * 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] | 2429 | */ |
| 2430 | const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags) |
| 2431 | { |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2432 | unsigned long long imult, idiv; |
| 2433 | unsigned long long omult, odiv; |
| 2434 | unsigned long long value, result; |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2435 | const char *str = text; |
| 2436 | |
| 2437 | if (!isdigit((unsigned char)*text)) |
| 2438 | return text; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2439 | |
| 2440 | omult = odiv = 1; |
| 2441 | |
| 2442 | switch (unit_flags & TIME_UNIT_MASK) { |
| 2443 | case TIME_UNIT_US: omult = 1000000; break; |
| 2444 | case TIME_UNIT_MS: omult = 1000; break; |
| 2445 | case TIME_UNIT_S: break; |
| 2446 | case TIME_UNIT_MIN: odiv = 60; break; |
| 2447 | case TIME_UNIT_HOUR: odiv = 3600; break; |
| 2448 | case TIME_UNIT_DAY: odiv = 86400; break; |
| 2449 | default: break; |
| 2450 | } |
| 2451 | |
| 2452 | value = 0; |
| 2453 | |
| 2454 | while (1) { |
| 2455 | unsigned int j; |
| 2456 | |
| 2457 | j = *text - '0'; |
| 2458 | if (j > 9) |
| 2459 | break; |
| 2460 | text++; |
| 2461 | value *= 10; |
| 2462 | value += j; |
| 2463 | } |
| 2464 | |
| 2465 | imult = idiv = 1; |
| 2466 | switch (*text) { |
| 2467 | case '\0': /* no unit = default unit */ |
| 2468 | imult = omult = idiv = odiv = 1; |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2469 | goto end; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2470 | case 's': /* second = unscaled unit */ |
| 2471 | break; |
| 2472 | case 'u': /* microsecond : "us" */ |
| 2473 | if (text[1] == 's') { |
| 2474 | idiv = 1000000; |
| 2475 | text++; |
Thayne McCombs | a683805 | 2021-04-02 14:12:43 -0600 | [diff] [blame] | 2476 | break; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2477 | } |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2478 | return text; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2479 | case 'm': /* millisecond : "ms" or minute: "m" */ |
| 2480 | if (text[1] == 's') { |
| 2481 | idiv = 1000; |
| 2482 | text++; |
| 2483 | } else |
| 2484 | imult = 60; |
| 2485 | break; |
| 2486 | case 'h': /* hour : "h" */ |
| 2487 | imult = 3600; |
| 2488 | break; |
| 2489 | case 'd': /* day : "d" */ |
| 2490 | imult = 86400; |
| 2491 | break; |
| 2492 | default: |
| 2493 | return text; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2494 | } |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2495 | if (*(++text) != '\0') { |
| 2496 | ha_warning("unexpected character '%c' after the timer value '%s', only " |
| 2497 | "(us=microseconds,ms=milliseconds,s=seconds,m=minutes,h=hours,d=days) are supported." |
| 2498 | " This will be reported as an error in next versions.\n", *text, str); |
| 2499 | } |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2500 | |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2501 | end: |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2502 | if (omult % idiv == 0) { omult /= idiv; idiv = 1; } |
| 2503 | if (idiv % omult == 0) { idiv /= omult; omult = 1; } |
| 2504 | if (imult % odiv == 0) { imult /= odiv; odiv = 1; } |
| 2505 | if (odiv % imult == 0) { odiv /= imult; imult = 1; } |
| 2506 | |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2507 | result = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv); |
| 2508 | if (result >= 0x80000000) |
| 2509 | return PARSE_TIME_OVER; |
| 2510 | if (!result && value) |
| 2511 | return PARSE_TIME_UNDER; |
| 2512 | *ret = result; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2513 | return NULL; |
| 2514 | } |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2515 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2516 | /* this function converts the string starting at <text> to an unsigned int |
| 2517 | * 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] | 2518 | * character is returned. If the conversion is successful, NULL is returned. |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2519 | */ |
| 2520 | const char *parse_size_err(const char *text, unsigned *ret) { |
| 2521 | unsigned value = 0; |
| 2522 | |
Christopher Faulet | 82635a0 | 2020-12-11 09:30:45 +0100 | [diff] [blame] | 2523 | if (!isdigit((unsigned char)*text)) |
| 2524 | return text; |
| 2525 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2526 | while (1) { |
| 2527 | unsigned int j; |
| 2528 | |
| 2529 | j = *text - '0'; |
| 2530 | if (j > 9) |
| 2531 | break; |
| 2532 | if (value > ~0U / 10) |
| 2533 | return text; |
| 2534 | value *= 10; |
| 2535 | if (value > (value + j)) |
| 2536 | return text; |
| 2537 | value += j; |
| 2538 | text++; |
| 2539 | } |
| 2540 | |
| 2541 | switch (*text) { |
| 2542 | case '\0': |
| 2543 | break; |
| 2544 | case 'K': |
| 2545 | case 'k': |
| 2546 | if (value > ~0U >> 10) |
| 2547 | return text; |
| 2548 | value = value << 10; |
| 2549 | break; |
| 2550 | case 'M': |
| 2551 | case 'm': |
| 2552 | if (value > ~0U >> 20) |
| 2553 | return text; |
| 2554 | value = value << 20; |
| 2555 | break; |
| 2556 | case 'G': |
| 2557 | case 'g': |
| 2558 | if (value > ~0U >> 30) |
| 2559 | return text; |
| 2560 | value = value << 30; |
| 2561 | break; |
| 2562 | default: |
| 2563 | return text; |
| 2564 | } |
| 2565 | |
Godbach | 58048a2 | 2015-01-28 17:36:16 +0800 | [diff] [blame] | 2566 | if (*text != '\0' && *++text != '\0') |
| 2567 | return text; |
| 2568 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2569 | *ret = value; |
| 2570 | return NULL; |
| 2571 | } |
| 2572 | |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2573 | /* |
| 2574 | * Parse binary string written in hexadecimal (source) and store the decoded |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2575 | * 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] | 2576 | * 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] | 2577 | * error message in err. In success case, it returns the consumed length. |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2578 | */ |
| 2579 | int parse_binary(const char *source, char **binstr, int *binstrlen, char **err) |
| 2580 | { |
| 2581 | int len; |
| 2582 | const char *p = source; |
| 2583 | int i,j; |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2584 | int alloc; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2585 | |
| 2586 | len = strlen(source); |
| 2587 | if (len % 2) { |
| 2588 | memprintf(err, "an even number of hex digit is expected"); |
| 2589 | return 0; |
| 2590 | } |
| 2591 | |
| 2592 | len = len >> 1; |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2593 | |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2594 | if (!*binstr) { |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 2595 | *binstr = calloc(len, sizeof(**binstr)); |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2596 | if (!*binstr) { |
| 2597 | memprintf(err, "out of memory while loading string pattern"); |
| 2598 | return 0; |
| 2599 | } |
| 2600 | alloc = 1; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2601 | } |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2602 | else { |
| 2603 | if (*binstrlen < len) { |
Joseph Herlant | 76dbe78 | 2018-11-15 12:01:22 -0800 | [diff] [blame] | 2604 | memprintf(err, "no space available in the buffer. expect %d, provides %d", |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2605 | len, *binstrlen); |
| 2606 | return 0; |
| 2607 | } |
| 2608 | alloc = 0; |
| 2609 | } |
| 2610 | *binstrlen = len; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2611 | |
| 2612 | i = j = 0; |
| 2613 | while (j < len) { |
| 2614 | if (!ishex(p[i++])) |
| 2615 | goto bad_input; |
| 2616 | if (!ishex(p[i++])) |
| 2617 | goto bad_input; |
| 2618 | (*binstr)[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]); |
| 2619 | } |
Thierry FOURNIER | ee330af | 2014-01-21 11:36:14 +0100 | [diff] [blame] | 2620 | return len << 1; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2621 | |
| 2622 | bad_input: |
| 2623 | memprintf(err, "an hex digit is expected (found '%c')", p[i-1]); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2624 | if (alloc) |
| 2625 | ha_free(binstr); |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2626 | return 0; |
| 2627 | } |
| 2628 | |
Willy Tarreau | 946ba59 | 2009-05-10 15:41:18 +0200 | [diff] [blame] | 2629 | /* copies at most <n> characters from <src> and always terminates with '\0' */ |
| 2630 | char *my_strndup(const char *src, int n) |
| 2631 | { |
| 2632 | int len = 0; |
| 2633 | char *ret; |
| 2634 | |
| 2635 | while (len < n && src[len]) |
| 2636 | len++; |
| 2637 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2638 | ret = malloc(len + 1); |
Willy Tarreau | 946ba59 | 2009-05-10 15:41:18 +0200 | [diff] [blame] | 2639 | if (!ret) |
| 2640 | return ret; |
| 2641 | memcpy(ret, src, len); |
| 2642 | ret[len] = '\0'; |
| 2643 | return ret; |
| 2644 | } |
| 2645 | |
Baptiste Assmann | bb77c8e | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2646 | /* |
| 2647 | * search needle in haystack |
| 2648 | * returns the pointer if found, returns NULL otherwise |
| 2649 | */ |
| 2650 | const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) |
| 2651 | { |
| 2652 | const void *c = NULL; |
| 2653 | unsigned char f; |
| 2654 | |
| 2655 | if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen)) |
| 2656 | return NULL; |
| 2657 | |
| 2658 | f = *(char *)needle; |
| 2659 | c = haystack; |
| 2660 | while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) { |
| 2661 | if ((haystacklen - (c - haystack)) < needlelen) |
| 2662 | return NULL; |
| 2663 | |
| 2664 | if (memcmp(c, needle, needlelen) == 0) |
| 2665 | return c; |
| 2666 | ++c; |
| 2667 | } |
| 2668 | return NULL; |
| 2669 | } |
| 2670 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 2671 | /* get length of the initial segment consisting entirely of bytes in <accept> */ |
Christopher Faulet | 5eb96cb | 2020-04-15 10:23:01 +0200 | [diff] [blame] | 2672 | size_t my_memspn(const void *str, size_t len, const void *accept, size_t acceptlen) |
| 2673 | { |
| 2674 | size_t ret = 0; |
| 2675 | |
| 2676 | while (ret < len && memchr(accept, *((int *)str), acceptlen)) { |
| 2677 | str++; |
| 2678 | ret++; |
| 2679 | } |
| 2680 | return ret; |
| 2681 | } |
| 2682 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 2683 | /* 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] | 2684 | size_t my_memcspn(const void *str, size_t len, const void *reject, size_t rejectlen) |
| 2685 | { |
| 2686 | size_t ret = 0; |
| 2687 | |
| 2688 | while (ret < len) { |
| 2689 | if(memchr(reject, *((int *)str), rejectlen)) |
| 2690 | return ret; |
| 2691 | str++; |
| 2692 | ret++; |
| 2693 | } |
| 2694 | return ret; |
| 2695 | } |
| 2696 | |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 2697 | /* This function returns the first unused key greater than or equal to <key> in |
| 2698 | * ID tree <root>. Zero is returned if no place is found. |
| 2699 | */ |
| 2700 | unsigned int get_next_id(struct eb_root *root, unsigned int key) |
| 2701 | { |
| 2702 | struct eb32_node *used; |
| 2703 | |
| 2704 | do { |
| 2705 | used = eb32_lookup_ge(root, key); |
| 2706 | if (!used || used->key > key) |
| 2707 | return key; /* key is available */ |
| 2708 | key++; |
| 2709 | } while (key); |
| 2710 | return key; |
| 2711 | } |
| 2712 | |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2713 | /* dump the full tree to <file> in DOT format for debugging purposes. Will |
| 2714 | * optionally highlight node <subj> if found, depending on operation <op> : |
| 2715 | * 0 : nothing |
| 2716 | * >0 : insertion, node/leaf are surrounded in red |
| 2717 | * <0 : removal, node/leaf are dashed with no background |
| 2718 | * Will optionally add "desc" as a label on the graph if set and non-null. |
| 2719 | */ |
| 2720 | 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] | 2721 | { |
| 2722 | struct eb32sc_node *node; |
| 2723 | unsigned long scope = -1; |
| 2724 | |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2725 | fprintf(file, "digraph ebtree {\n"); |
| 2726 | |
| 2727 | if (desc && *desc) { |
| 2728 | fprintf(file, |
| 2729 | " fontname=\"fixed\";\n" |
| 2730 | " fontsize=8;\n" |
| 2731 | " label=\"%s\";\n", desc); |
| 2732 | } |
| 2733 | |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2734 | fprintf(file, |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2735 | " node [fontname=\"fixed\" fontsize=8 shape=\"box\" style=\"filled\" color=\"black\" fillcolor=\"white\"];\n" |
| 2736 | " edge [fontname=\"fixed\" fontsize=8 style=\"solid\" color=\"magenta\" dir=\"forward\"];\n" |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2737 | " \"%lx_n\" [label=\"root\\n%lx\"]\n", (long)eb_root_to_node(root), (long)root |
| 2738 | ); |
| 2739 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2740 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"L\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2741 | (long)eb_root_to_node(root), |
| 2742 | (long)eb_root_to_node(eb_clrtag(root->b[0])), |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2743 | eb_gettag(root->b[0]) == EB_LEAF ? 'l' : 'n'); |
| 2744 | |
| 2745 | node = eb32sc_first(root, scope); |
| 2746 | while (node) { |
| 2747 | if (node->node.node_p) { |
| 2748 | /* node part is used */ |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2749 | fprintf(file, " \"%lx_n\" [label=\"%lx\\nkey=%u\\nscope=%lx\\nbit=%d\" fillcolor=\"lightskyblue1\" %s];\n", |
| 2750 | (long)node, (long)node, node->key, node->node_s, node->node.bit, |
| 2751 | (node == subj) ? (op < 0 ? "color=\"red\" style=\"dashed\"" : op > 0 ? "color=\"red\"" : "") : ""); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2752 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2753 | fprintf(file, " \"%lx_n\" -> \"%lx_n\" [taillabel=\"%c\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2754 | (long)node, |
| 2755 | (long)eb_root_to_node(eb_clrtag(node->node.node_p)), |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2756 | eb_gettag(node->node.node_p) ? 'R' : 'L'); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2757 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2758 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"L\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2759 | (long)node, |
| 2760 | (long)eb_root_to_node(eb_clrtag(node->node.branches.b[0])), |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2761 | eb_gettag(node->node.branches.b[0]) == EB_LEAF ? 'l' : 'n'); |
| 2762 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2763 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"R\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2764 | (long)node, |
| 2765 | (long)eb_root_to_node(eb_clrtag(node->node.branches.b[1])), |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2766 | eb_gettag(node->node.branches.b[1]) == EB_LEAF ? 'l' : 'n'); |
| 2767 | } |
| 2768 | |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2769 | fprintf(file, " \"%lx_l\" [label=\"%lx\\nkey=%u\\nscope=%lx\\npfx=%u\" fillcolor=\"yellow\" %s];\n", |
| 2770 | (long)node, (long)node, node->key, node->leaf_s, node->node.pfx, |
| 2771 | (node == subj) ? (op < 0 ? "color=\"red\" style=\"dashed\"" : op > 0 ? "color=\"red\"" : "") : ""); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2772 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2773 | fprintf(file, " \"%lx_l\" -> \"%lx_n\" [taillabel=\"%c\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2774 | (long)node, |
| 2775 | (long)eb_root_to_node(eb_clrtag(node->node.leaf_p)), |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2776 | eb_gettag(node->node.leaf_p) ? 'R' : 'L'); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2777 | node = eb32sc_next(node, scope); |
| 2778 | } |
| 2779 | fprintf(file, "}\n"); |
| 2780 | } |
| 2781 | |
Willy Tarreau | 0dc9e6d | 2022-08-01 11:55:57 +0200 | [diff] [blame] | 2782 | /* dump the full tree to <file> in DOT format for debugging purposes. Will |
| 2783 | * optionally highlight node <subj> if found, depending on operation <op> : |
| 2784 | * 0 : nothing |
| 2785 | * >0 : insertion, node/leaf are surrounded in red |
| 2786 | * <0 : removal, node/leaf are dashed with no background |
| 2787 | * Will optionally add "desc" as a label on the graph if set and non-null. The |
| 2788 | * key is printed as a u32 hex value. A full-sized hex dump would be better but |
| 2789 | * is left to be implemented. |
| 2790 | */ |
| 2791 | void ebmb_to_file(FILE *file, struct eb_root *root, const struct ebmb_node *subj, int op, const char *desc) |
| 2792 | { |
| 2793 | struct ebmb_node *node; |
| 2794 | |
| 2795 | fprintf(file, "digraph ebtree {\n"); |
| 2796 | |
| 2797 | if (desc && *desc) { |
| 2798 | fprintf(file, |
| 2799 | " fontname=\"fixed\";\n" |
| 2800 | " fontsize=8;\n" |
| 2801 | " label=\"%s\";\n", desc); |
| 2802 | } |
| 2803 | |
| 2804 | fprintf(file, |
| 2805 | " node [fontname=\"fixed\" fontsize=8 shape=\"box\" style=\"filled\" color=\"black\" fillcolor=\"white\"];\n" |
| 2806 | " edge [fontname=\"fixed\" fontsize=8 style=\"solid\" color=\"magenta\" dir=\"forward\"];\n" |
| 2807 | " \"%lx_n\" [label=\"root\\n%lx\"]\n", (long)eb_root_to_node(root), (long)root |
| 2808 | ); |
| 2809 | |
| 2810 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"L\"];\n", |
| 2811 | (long)eb_root_to_node(root), |
| 2812 | (long)eb_root_to_node(eb_clrtag(root->b[0])), |
| 2813 | eb_gettag(root->b[0]) == EB_LEAF ? 'l' : 'n'); |
| 2814 | |
| 2815 | node = ebmb_first(root); |
| 2816 | while (node) { |
| 2817 | if (node->node.node_p) { |
| 2818 | /* node part is used */ |
| 2819 | fprintf(file, " \"%lx_n\" [label=\"%lx\\nkey=%#x\\nbit=%d\" fillcolor=\"lightskyblue1\" %s];\n", |
| 2820 | (long)node, (long)node, read_u32(node->key), node->node.bit, |
| 2821 | (node == subj) ? (op < 0 ? "color=\"red\" style=\"dashed\"" : op > 0 ? "color=\"red\"" : "") : ""); |
| 2822 | |
| 2823 | fprintf(file, " \"%lx_n\" -> \"%lx_n\" [taillabel=\"%c\"];\n", |
| 2824 | (long)node, |
| 2825 | (long)eb_root_to_node(eb_clrtag(node->node.node_p)), |
| 2826 | eb_gettag(node->node.node_p) ? 'R' : 'L'); |
| 2827 | |
| 2828 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"L\"];\n", |
| 2829 | (long)node, |
| 2830 | (long)eb_root_to_node(eb_clrtag(node->node.branches.b[0])), |
| 2831 | eb_gettag(node->node.branches.b[0]) == EB_LEAF ? 'l' : 'n'); |
| 2832 | |
| 2833 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"R\"];\n", |
| 2834 | (long)node, |
| 2835 | (long)eb_root_to_node(eb_clrtag(node->node.branches.b[1])), |
| 2836 | eb_gettag(node->node.branches.b[1]) == EB_LEAF ? 'l' : 'n'); |
| 2837 | } |
| 2838 | |
| 2839 | fprintf(file, " \"%lx_l\" [label=\"%lx\\nkey=%#x\\npfx=%u\" fillcolor=\"yellow\" %s];\n", |
| 2840 | (long)node, (long)node, read_u32(node->key), node->node.pfx, |
| 2841 | (node == subj) ? (op < 0 ? "color=\"red\" style=\"dashed\"" : op > 0 ? "color=\"red\"" : "") : ""); |
| 2842 | |
| 2843 | fprintf(file, " \"%lx_l\" -> \"%lx_n\" [taillabel=\"%c\"];\n", |
| 2844 | (long)node, |
| 2845 | (long)eb_root_to_node(eb_clrtag(node->node.leaf_p)), |
| 2846 | eb_gettag(node->node.leaf_p) ? 'R' : 'L'); |
| 2847 | node = ebmb_next(node); |
| 2848 | } |
| 2849 | fprintf(file, "}\n"); |
| 2850 | } |
| 2851 | |
Willy Tarreau | 348238b | 2010-01-18 15:05:57 +0100 | [diff] [blame] | 2852 | /* This function compares a sample word possibly followed by blanks to another |
| 2853 | * clean word. The compare is case-insensitive. 1 is returned if both are equal, |
| 2854 | * otherwise zero. This intends to be used when checking HTTP headers for some |
| 2855 | * values. Note that it validates a word followed only by blanks but does not |
| 2856 | * validate a word followed by blanks then other chars. |
| 2857 | */ |
| 2858 | int word_match(const char *sample, int slen, const char *word, int wlen) |
| 2859 | { |
| 2860 | if (slen < wlen) |
| 2861 | return 0; |
| 2862 | |
| 2863 | while (wlen) { |
| 2864 | char c = *sample ^ *word; |
| 2865 | if (c && c != ('A' ^ 'a')) |
| 2866 | return 0; |
| 2867 | sample++; |
| 2868 | word++; |
| 2869 | slen--; |
| 2870 | wlen--; |
| 2871 | } |
| 2872 | |
| 2873 | while (slen) { |
| 2874 | if (*sample != ' ' && *sample != '\t') |
| 2875 | return 0; |
| 2876 | sample++; |
| 2877 | slen--; |
| 2878 | } |
| 2879 | return 1; |
| 2880 | } |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 2881 | |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2882 | /* Converts any text-formatted IPv4 address to a host-order IPv4 address. It |
| 2883 | * is particularly fast because it avoids expensive operations such as |
| 2884 | * multiplies, which are optimized away at the end. It requires a properly |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2885 | * formatted address though (3 points). |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2886 | */ |
| 2887 | unsigned int inetaddr_host(const char *text) |
| 2888 | { |
| 2889 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 2890 | register unsigned int dig100, dig10, dig1; |
| 2891 | int s; |
| 2892 | const char *p, *d; |
| 2893 | |
| 2894 | dig1 = dig10 = dig100 = ascii_zero; |
| 2895 | s = 24; |
| 2896 | |
| 2897 | p = text; |
| 2898 | while (1) { |
| 2899 | if (((unsigned)(*p - '0')) <= 9) { |
| 2900 | p++; |
| 2901 | continue; |
| 2902 | } |
| 2903 | |
| 2904 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 2905 | if (p == text) |
| 2906 | goto end; |
| 2907 | |
| 2908 | d = p - 1; |
| 2909 | dig1 |= (unsigned int)(*d << s); |
| 2910 | if (d == text) |
| 2911 | goto end; |
| 2912 | |
| 2913 | d--; |
| 2914 | dig10 |= (unsigned int)(*d << s); |
| 2915 | if (d == text) |
| 2916 | goto end; |
| 2917 | |
| 2918 | d--; |
| 2919 | dig100 |= (unsigned int)(*d << s); |
| 2920 | end: |
| 2921 | if (!s || *p != '.') |
| 2922 | break; |
| 2923 | |
| 2924 | s -= 8; |
| 2925 | text = ++p; |
| 2926 | } |
| 2927 | |
| 2928 | dig100 -= ascii_zero; |
| 2929 | dig10 -= ascii_zero; |
| 2930 | dig1 -= ascii_zero; |
| 2931 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 2932 | } |
| 2933 | |
| 2934 | /* |
| 2935 | * Idem except the first unparsed character has to be passed in <stop>. |
| 2936 | */ |
| 2937 | unsigned int inetaddr_host_lim(const char *text, const char *stop) |
| 2938 | { |
| 2939 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 2940 | register unsigned int dig100, dig10, dig1; |
| 2941 | int s; |
| 2942 | const char *p, *d; |
| 2943 | |
| 2944 | dig1 = dig10 = dig100 = ascii_zero; |
| 2945 | s = 24; |
| 2946 | |
| 2947 | p = text; |
| 2948 | while (1) { |
| 2949 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 2950 | p++; |
| 2951 | continue; |
| 2952 | } |
| 2953 | |
| 2954 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 2955 | if (p == text) |
| 2956 | goto end; |
| 2957 | |
| 2958 | d = p - 1; |
| 2959 | dig1 |= (unsigned int)(*d << s); |
| 2960 | if (d == text) |
| 2961 | goto end; |
| 2962 | |
| 2963 | d--; |
| 2964 | dig10 |= (unsigned int)(*d << s); |
| 2965 | if (d == text) |
| 2966 | goto end; |
| 2967 | |
| 2968 | d--; |
| 2969 | dig100 |= (unsigned int)(*d << s); |
| 2970 | end: |
| 2971 | if (!s || p == stop || *p != '.') |
| 2972 | break; |
| 2973 | |
| 2974 | s -= 8; |
| 2975 | text = ++p; |
| 2976 | } |
| 2977 | |
| 2978 | dig100 -= ascii_zero; |
| 2979 | dig10 -= ascii_zero; |
| 2980 | dig1 -= ascii_zero; |
| 2981 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 2982 | } |
| 2983 | |
| 2984 | /* |
| 2985 | * Idem except the pointer to first unparsed byte is returned into <ret> which |
| 2986 | * must not be NULL. |
| 2987 | */ |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 2988 | unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret) |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2989 | { |
| 2990 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 2991 | register unsigned int dig100, dig10, dig1; |
| 2992 | int s; |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 2993 | char *p, *d; |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2994 | |
| 2995 | dig1 = dig10 = dig100 = ascii_zero; |
| 2996 | s = 24; |
| 2997 | |
| 2998 | p = text; |
| 2999 | while (1) { |
| 3000 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 3001 | p++; |
| 3002 | continue; |
| 3003 | } |
| 3004 | |
| 3005 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 3006 | if (p == text) |
| 3007 | goto end; |
| 3008 | |
| 3009 | d = p - 1; |
| 3010 | dig1 |= (unsigned int)(*d << s); |
| 3011 | if (d == text) |
| 3012 | goto end; |
| 3013 | |
| 3014 | d--; |
| 3015 | dig10 |= (unsigned int)(*d << s); |
| 3016 | if (d == text) |
| 3017 | goto end; |
| 3018 | |
| 3019 | d--; |
| 3020 | dig100 |= (unsigned int)(*d << s); |
| 3021 | end: |
| 3022 | if (!s || p == stop || *p != '.') |
| 3023 | break; |
| 3024 | |
| 3025 | s -= 8; |
| 3026 | text = ++p; |
| 3027 | } |
| 3028 | |
| 3029 | *ret = p; |
| 3030 | dig100 -= ascii_zero; |
| 3031 | dig10 -= ascii_zero; |
| 3032 | dig1 -= ascii_zero; |
| 3033 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 3034 | } |
| 3035 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 3036 | /* Convert a fixed-length string to an IP address. Returns 0 in case of error, |
| 3037 | * or the number of chars read in case of success. Maybe this could be replaced |
| 3038 | * by one of the functions above. Also, apparently this function does not support |
| 3039 | * hosts above 255 and requires exactly 4 octets. |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 3040 | * The destination is only modified on success. |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 3041 | */ |
| 3042 | int buf2ip(const char *buf, size_t len, struct in_addr *dst) |
| 3043 | { |
| 3044 | const char *addr; |
| 3045 | int saw_digit, octets, ch; |
| 3046 | u_char tmp[4], *tp; |
| 3047 | const char *cp = buf; |
| 3048 | |
| 3049 | saw_digit = 0; |
| 3050 | octets = 0; |
| 3051 | *(tp = tmp) = 0; |
| 3052 | |
| 3053 | for (addr = buf; addr - buf < len; addr++) { |
| 3054 | unsigned char digit = (ch = *addr) - '0'; |
| 3055 | |
| 3056 | if (digit > 9 && ch != '.') |
| 3057 | break; |
| 3058 | |
| 3059 | if (digit <= 9) { |
| 3060 | u_int new = *tp * 10 + digit; |
| 3061 | |
| 3062 | if (new > 255) |
| 3063 | return 0; |
| 3064 | |
| 3065 | *tp = new; |
| 3066 | |
| 3067 | if (!saw_digit) { |
| 3068 | if (++octets > 4) |
| 3069 | return 0; |
| 3070 | saw_digit = 1; |
| 3071 | } |
| 3072 | } else if (ch == '.' && saw_digit) { |
| 3073 | if (octets == 4) |
| 3074 | return 0; |
| 3075 | |
| 3076 | *++tp = 0; |
| 3077 | saw_digit = 0; |
| 3078 | } else |
| 3079 | return 0; |
| 3080 | } |
| 3081 | |
| 3082 | if (octets < 4) |
| 3083 | return 0; |
| 3084 | |
| 3085 | memcpy(&dst->s_addr, tmp, 4); |
| 3086 | return addr - cp; |
| 3087 | } |
| 3088 | |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3089 | /* This function converts the string in <buf> of the len <len> to |
| 3090 | * struct in6_addr <dst> which must be allocated by the caller. |
| 3091 | * This function returns 1 in success case, otherwise zero. |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 3092 | * The destination is only modified on success. |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3093 | */ |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3094 | int buf2ip6(const char *buf, size_t len, struct in6_addr *dst) |
| 3095 | { |
Thierry FOURNIER | cd65991 | 2013-12-11 12:33:54 +0100 | [diff] [blame] | 3096 | char null_term_ip6[INET6_ADDRSTRLEN + 1]; |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 3097 | struct in6_addr out; |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3098 | |
Thierry FOURNIER | cd65991 | 2013-12-11 12:33:54 +0100 | [diff] [blame] | 3099 | if (len > INET6_ADDRSTRLEN) |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3100 | return 0; |
| 3101 | |
| 3102 | memcpy(null_term_ip6, buf, len); |
| 3103 | null_term_ip6[len] = '\0'; |
| 3104 | |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 3105 | if (!inet_pton(AF_INET6, null_term_ip6, &out)) |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3106 | return 0; |
| 3107 | |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 3108 | *dst = out; |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3109 | return 1; |
| 3110 | } |
| 3111 | |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 3112 | /* To be used to quote config arg positions. Returns the short string at <ptr> |
| 3113 | * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line" |
| 3114 | * if ptr is NULL or empty. The string is locally allocated. |
| 3115 | */ |
| 3116 | const char *quote_arg(const char *ptr) |
| 3117 | { |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 3118 | static THREAD_LOCAL char val[32]; |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 3119 | int i; |
| 3120 | |
| 3121 | if (!ptr || !*ptr) |
| 3122 | return "end of line"; |
| 3123 | val[0] = '\''; |
Willy Tarreau | de2dd6b | 2013-01-24 02:14:42 +0100 | [diff] [blame] | 3124 | for (i = 1; i < sizeof(val) - 2 && *ptr; i++) |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 3125 | val[i] = *ptr++; |
| 3126 | val[i++] = '\''; |
| 3127 | val[i] = '\0'; |
| 3128 | return val; |
| 3129 | } |
| 3130 | |
Willy Tarreau | 5b18020 | 2010-07-18 10:40:48 +0200 | [diff] [blame] | 3131 | /* returns an operator among STD_OP_* for string <str> or < 0 if unknown */ |
| 3132 | int get_std_op(const char *str) |
| 3133 | { |
| 3134 | int ret = -1; |
| 3135 | |
| 3136 | if (*str == 'e' && str[1] == 'q') |
| 3137 | ret = STD_OP_EQ; |
| 3138 | else if (*str == 'n' && str[1] == 'e') |
| 3139 | ret = STD_OP_NE; |
| 3140 | else if (*str == 'l') { |
| 3141 | if (str[1] == 'e') ret = STD_OP_LE; |
| 3142 | else if (str[1] == 't') ret = STD_OP_LT; |
| 3143 | } |
| 3144 | else if (*str == 'g') { |
| 3145 | if (str[1] == 'e') ret = STD_OP_GE; |
| 3146 | else if (str[1] == 't') ret = STD_OP_GT; |
| 3147 | } |
| 3148 | |
| 3149 | if (ret == -1 || str[2] != '\0') |
| 3150 | return -1; |
| 3151 | return ret; |
| 3152 | } |
| 3153 | |
Willy Tarreau | 4c14eaa | 2010-11-24 14:01:45 +0100 | [diff] [blame] | 3154 | /* hash a 32-bit integer to another 32-bit integer */ |
| 3155 | unsigned int full_hash(unsigned int a) |
| 3156 | { |
| 3157 | return __full_hash(a); |
| 3158 | } |
| 3159 | |
Willy Tarreau | f324111 | 2019-02-26 09:56:22 +0100 | [diff] [blame] | 3160 | /* Return the bit position in mask <m> of the nth bit set of rank <r>, between |
| 3161 | * 0 and LONGBITS-1 included, starting from the left. For example ranks 0,1,2,3 |
| 3162 | * for mask 0x55 will be 6, 4, 2 and 0 respectively. This algorithm is based on |
| 3163 | * a popcount variant and is described here : |
| 3164 | * https://graphics.stanford.edu/~seander/bithacks.html |
| 3165 | */ |
| 3166 | unsigned int mask_find_rank_bit(unsigned int r, unsigned long m) |
| 3167 | { |
| 3168 | unsigned long a, b, c, d; |
| 3169 | unsigned int s; |
| 3170 | unsigned int t; |
| 3171 | |
| 3172 | a = m - ((m >> 1) & ~0UL/3); |
| 3173 | b = (a & ~0UL/5) + ((a >> 2) & ~0UL/5); |
| 3174 | c = (b + (b >> 4)) & ~0UL/0x11; |
| 3175 | d = (c + (c >> 8)) & ~0UL/0x101; |
| 3176 | |
| 3177 | r++; // make r be 1..64 |
| 3178 | |
| 3179 | t = 0; |
| 3180 | s = LONGBITS; |
| 3181 | if (s > 32) { |
Willy Tarreau | 9b6be3b | 2019-03-18 16:31:18 +0100 | [diff] [blame] | 3182 | unsigned long d2 = (d >> 16) >> 16; |
| 3183 | t = d2 + (d2 >> 16); |
Willy Tarreau | f324111 | 2019-02-26 09:56:22 +0100 | [diff] [blame] | 3184 | s -= ((t - r) & 256) >> 3; r -= (t & ((t - r) >> 8)); |
| 3185 | } |
| 3186 | |
| 3187 | t = (d >> (s - 16)) & 0xff; |
| 3188 | s -= ((t - r) & 256) >> 4; r -= (t & ((t - r) >> 8)); |
| 3189 | t = (c >> (s - 8)) & 0xf; |
| 3190 | s -= ((t - r) & 256) >> 5; r -= (t & ((t - r) >> 8)); |
| 3191 | t = (b >> (s - 4)) & 0x7; |
| 3192 | s -= ((t - r) & 256) >> 6; r -= (t & ((t - r) >> 8)); |
| 3193 | t = (a >> (s - 2)) & 0x3; |
| 3194 | s -= ((t - r) & 256) >> 7; r -= (t & ((t - r) >> 8)); |
| 3195 | t = (m >> (s - 1)) & 0x1; |
| 3196 | s -= ((t - r) & 256) >> 8; |
| 3197 | |
| 3198 | return s - 1; |
| 3199 | } |
| 3200 | |
| 3201 | /* Same as mask_find_rank_bit() above but makes use of pre-computed bitmaps |
| 3202 | * based on <m>, in <a..d>. These ones must be updated whenever <m> changes |
| 3203 | * using mask_prep_rank_map() below. |
| 3204 | */ |
| 3205 | unsigned int mask_find_rank_bit_fast(unsigned int r, unsigned long m, |
| 3206 | unsigned long a, unsigned long b, |
| 3207 | unsigned long c, unsigned long d) |
| 3208 | { |
| 3209 | unsigned int s; |
| 3210 | unsigned int t; |
| 3211 | |
| 3212 | r++; // make r be 1..64 |
| 3213 | |
| 3214 | t = 0; |
| 3215 | s = LONGBITS; |
| 3216 | if (s > 32) { |
Willy Tarreau | 9b6be3b | 2019-03-18 16:31:18 +0100 | [diff] [blame] | 3217 | unsigned long d2 = (d >> 16) >> 16; |
| 3218 | t = d2 + (d2 >> 16); |
Willy Tarreau | f324111 | 2019-02-26 09:56:22 +0100 | [diff] [blame] | 3219 | s -= ((t - r) & 256) >> 3; r -= (t & ((t - r) >> 8)); |
| 3220 | } |
| 3221 | |
| 3222 | t = (d >> (s - 16)) & 0xff; |
| 3223 | s -= ((t - r) & 256) >> 4; r -= (t & ((t - r) >> 8)); |
| 3224 | t = (c >> (s - 8)) & 0xf; |
| 3225 | s -= ((t - r) & 256) >> 5; r -= (t & ((t - r) >> 8)); |
| 3226 | t = (b >> (s - 4)) & 0x7; |
| 3227 | s -= ((t - r) & 256) >> 6; r -= (t & ((t - r) >> 8)); |
| 3228 | t = (a >> (s - 2)) & 0x3; |
| 3229 | s -= ((t - r) & 256) >> 7; r -= (t & ((t - r) >> 8)); |
| 3230 | t = (m >> (s - 1)) & 0x1; |
| 3231 | s -= ((t - r) & 256) >> 8; |
| 3232 | |
| 3233 | return s - 1; |
| 3234 | } |
| 3235 | |
| 3236 | /* Prepare the bitmaps used by the fast implementation of the find_rank_bit() |
| 3237 | * above. |
| 3238 | */ |
| 3239 | void mask_prep_rank_map(unsigned long m, |
| 3240 | unsigned long *a, unsigned long *b, |
| 3241 | unsigned long *c, unsigned long *d) |
| 3242 | { |
| 3243 | *a = m - ((m >> 1) & ~0UL/3); |
| 3244 | *b = (*a & ~0UL/5) + ((*a >> 2) & ~0UL/5); |
| 3245 | *c = (*b + (*b >> 4)) & ~0UL/0x11; |
| 3246 | *d = (*c + (*c >> 8)) & ~0UL/0x101; |
| 3247 | } |
| 3248 | |
Willy Tarreau | c7a8a3c | 2022-06-21 20:19:54 +0200 | [diff] [blame] | 3249 | /* Returns the position of one bit set in <v>, starting at position <bit>, and |
| 3250 | * searching in other halves if not found. This is intended to be used to |
| 3251 | * report the position of one bit set among several based on a counter or a |
| 3252 | * random generator while preserving a relatively good distribution so that |
| 3253 | * values made of holes in the middle do not see one of the bits around the |
| 3254 | * hole being returned much more often than the other one. It can be seen as a |
| 3255 | * disturbed ffsl() where the initial search starts at bit <bit>. The look up |
| 3256 | * is performed in O(logN) time for N bit words, yielding a bit among 64 in |
| 3257 | * about 16 cycles. Its usage differs from the rank find function in that the |
| 3258 | * bit passed doesn't need to be limited to the value's popcount, making the |
| 3259 | * function easier to use for random picking, and twice as fast. Passing value |
| 3260 | * 0 for <v> makes no sense and -1 is returned in this case. |
| 3261 | */ |
| 3262 | int one_among_mask(unsigned long v, int bit) |
| 3263 | { |
| 3264 | /* note, these masks may be produced by ~0UL/((1UL<<scale)+1) but |
| 3265 | * that's more expensive. |
| 3266 | */ |
| 3267 | static const unsigned long halves[] = { |
| 3268 | (unsigned long)0x5555555555555555ULL, |
| 3269 | (unsigned long)0x3333333333333333ULL, |
| 3270 | (unsigned long)0x0F0F0F0F0F0F0F0FULL, |
| 3271 | (unsigned long)0x00FF00FF00FF00FFULL, |
| 3272 | (unsigned long)0x0000FFFF0000FFFFULL, |
| 3273 | (unsigned long)0x00000000FFFFFFFFULL |
| 3274 | }; |
| 3275 | unsigned long halfword = ~0UL; |
| 3276 | int scope = 0; |
| 3277 | int mirror; |
| 3278 | int scale; |
| 3279 | |
| 3280 | if (!v) |
| 3281 | return -1; |
| 3282 | |
| 3283 | /* we check if the exact bit is set or if it's present in a mirror |
| 3284 | * position based on the current scale we're checking, in which case |
| 3285 | * it's returned with its current (or mirrored) value. Otherwise we'll |
| 3286 | * make sure there's at least one bit in the half we're in, and will |
| 3287 | * scale down to a smaller scope and try again, until we find the |
| 3288 | * closest bit. |
| 3289 | */ |
| 3290 | for (scale = (sizeof(long) > 4) ? 5 : 4; scale >= 0; scale--) { |
| 3291 | halfword >>= (1UL << scale); |
| 3292 | scope |= (1UL << scale); |
| 3293 | mirror = bit ^ (1UL << scale); |
| 3294 | if (v & ((1UL << bit) | (1UL << mirror))) |
| 3295 | return (v & (1UL << bit)) ? bit : mirror; |
| 3296 | |
| 3297 | if (!((v >> (bit & scope)) & halves[scale] & halfword)) |
| 3298 | bit = mirror; |
| 3299 | } |
| 3300 | return bit; |
| 3301 | } |
| 3302 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3303 | /* Return non-zero if IPv4 address is part of the network, |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3304 | * otherwise zero. Note that <addr> may not necessarily be aligned |
| 3305 | * while the two other ones must. |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3306 | */ |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3307 | 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] | 3308 | { |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3309 | struct in_addr addr_copy; |
| 3310 | |
| 3311 | memcpy(&addr_copy, addr, sizeof(addr_copy)); |
| 3312 | 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] | 3313 | } |
| 3314 | |
| 3315 | /* Return non-zero if IPv6 address is part of the network, |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3316 | * otherwise zero. Note that <addr> may not necessarily be aligned |
| 3317 | * while the two other ones must. |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3318 | */ |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3319 | 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] | 3320 | { |
| 3321 | int i; |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3322 | struct in6_addr addr_copy; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3323 | |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3324 | memcpy(&addr_copy, addr, sizeof(addr_copy)); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3325 | for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++) |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3326 | if (((((int *)&addr_copy)[i] & ((int *)mask)[i])) != |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3327 | (((int *)net)[i] & ((int *)mask)[i])) |
| 3328 | return 0; |
| 3329 | return 1; |
| 3330 | } |
| 3331 | |
| 3332 | /* RFC 4291 prefix */ |
| 3333 | const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00, |
| 3334 | 0x00, 0x00, 0x00, 0x00, |
| 3335 | 0x00, 0x00, 0xFF, 0xFF }; |
| 3336 | |
Joseph Herlant | 32b8327 | 2018-11-15 11:58:28 -0800 | [diff] [blame] | 3337 | /* Map IPv4 address on IPv6 address, as specified in RFC 3513. |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 3338 | * Input and output may overlap. |
| 3339 | */ |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3340 | void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr) |
| 3341 | { |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 3342 | struct in_addr tmp_addr; |
| 3343 | |
| 3344 | tmp_addr.s_addr = sin_addr->s_addr; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3345 | memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)); |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 3346 | memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3347 | } |
| 3348 | |
Joseph Herlant | 32b8327 | 2018-11-15 11:58:28 -0800 | [diff] [blame] | 3349 | /* Map IPv6 address on IPv4 address, as specified in RFC 3513. |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3350 | * Return true if conversion is possible and false otherwise. |
| 3351 | */ |
| 3352 | int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr) |
| 3353 | { |
| 3354 | if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) { |
| 3355 | memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]), |
| 3356 | sizeof(struct in_addr)); |
| 3357 | return 1; |
| 3358 | } |
| 3359 | |
| 3360 | return 0; |
| 3361 | } |
| 3362 | |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3363 | /* compare two struct sockaddr_storage, including port if <check_port> is true, |
| 3364 | * and return: |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3365 | * 0 (true) if the addr is the same in both |
| 3366 | * 1 (false) if the addr is not the same in both |
| 3367 | * -1 (unable) if one of the addr is not AF_INET* |
| 3368 | */ |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3369 | int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, int check_port) |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3370 | { |
| 3371 | if ((ss1->ss_family != AF_INET) && (ss1->ss_family != AF_INET6)) |
| 3372 | return -1; |
| 3373 | |
| 3374 | if ((ss2->ss_family != AF_INET) && (ss2->ss_family != AF_INET6)) |
| 3375 | return -1; |
| 3376 | |
| 3377 | if (ss1->ss_family != ss2->ss_family) |
| 3378 | return 1; |
| 3379 | |
| 3380 | switch (ss1->ss_family) { |
| 3381 | case AF_INET: |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3382 | return (memcmp(&((struct sockaddr_in *)ss1)->sin_addr, |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3383 | &((struct sockaddr_in *)ss2)->sin_addr, |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3384 | sizeof(struct in_addr)) != 0) || |
| 3385 | (check_port && get_net_port(ss1) != get_net_port(ss2)); |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3386 | case AF_INET6: |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3387 | return (memcmp(&((struct sockaddr_in6 *)ss1)->sin6_addr, |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3388 | &((struct sockaddr_in6 *)ss2)->sin6_addr, |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3389 | sizeof(struct in6_addr)) != 0) || |
| 3390 | (check_port && get_net_port(ss1) != get_net_port(ss2)); |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | return 1; |
| 3394 | } |
| 3395 | |
Christopher Faulet | 9553de7 | 2021-02-26 09:12:50 +0100 | [diff] [blame] | 3396 | /* compare a struct sockaddr_storage to a struct net_addr and return : |
| 3397 | * 0 (true) if <addr> is matching <net> |
| 3398 | * 1 (false) if <addr> is not matching <net> |
| 3399 | * -1 (unable) if <addr> or <net> is not AF_INET* |
| 3400 | */ |
| 3401 | int ipcmp2net(const struct sockaddr_storage *addr, const struct net_addr *net) |
| 3402 | { |
| 3403 | if ((addr->ss_family != AF_INET) && (addr->ss_family != AF_INET6)) |
| 3404 | return -1; |
| 3405 | |
| 3406 | if ((net->family != AF_INET) && (net->family != AF_INET6)) |
| 3407 | return -1; |
| 3408 | |
| 3409 | if (addr->ss_family != net->family) |
| 3410 | return 1; |
| 3411 | |
| 3412 | if (addr->ss_family == AF_INET && |
| 3413 | (((struct sockaddr_in *)addr)->sin_addr.s_addr & net->addr.v4.mask.s_addr) == net->addr.v4.ip.s_addr) |
| 3414 | return 0; |
| 3415 | else { |
| 3416 | const struct in6_addr *addr6 = &(((const struct sockaddr_in6*)addr)->sin6_addr); |
| 3417 | const struct in6_addr *nip6 = &net->addr.v6.ip; |
| 3418 | const struct in6_addr *nmask6 = &net->addr.v6.mask; |
| 3419 | |
| 3420 | if ((read_u32(&addr6->s6_addr[0]) & read_u32(&nmask6->s6_addr[0])) == read_u32(&nip6->s6_addr[0]) && |
| 3421 | (read_u32(&addr6->s6_addr[4]) & read_u32(&nmask6->s6_addr[4])) == read_u32(&nip6->s6_addr[4]) && |
| 3422 | (read_u32(&addr6->s6_addr[8]) & read_u32(&nmask6->s6_addr[8])) == read_u32(&nip6->s6_addr[8]) && |
| 3423 | (read_u32(&addr6->s6_addr[12]) & read_u32(&nmask6->s6_addr[12])) == read_u32(&nip6->s6_addr[12])) |
| 3424 | return 0; |
| 3425 | } |
| 3426 | |
| 3427 | return 1; |
| 3428 | } |
| 3429 | |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3430 | /* copy IP address from <source> into <dest> |
Willy Tarreau | dc3a9e8 | 2016-11-04 18:47:01 +0100 | [diff] [blame] | 3431 | * The caller must allocate and clear <dest> before calling. |
| 3432 | * The source must be in either AF_INET or AF_INET6 family, or the destination |
| 3433 | * address will be undefined. If the destination address used to hold a port, |
| 3434 | * it is preserved, so that this function can be used to switch to another |
| 3435 | * address family with no risk. Returns a pointer to the destination. |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3436 | */ |
| 3437 | struct sockaddr_storage *ipcpy(struct sockaddr_storage *source, struct sockaddr_storage *dest) |
| 3438 | { |
Willy Tarreau | dc3a9e8 | 2016-11-04 18:47:01 +0100 | [diff] [blame] | 3439 | int prev_port; |
| 3440 | |
| 3441 | prev_port = get_net_port(dest); |
| 3442 | memset(dest, 0, sizeof(*dest)); |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3443 | dest->ss_family = source->ss_family; |
| 3444 | |
| 3445 | /* copy new addr and apply it */ |
| 3446 | switch (source->ss_family) { |
| 3447 | case AF_INET: |
| 3448 | ((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] | 3449 | ((struct sockaddr_in *)dest)->sin_port = prev_port; |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3450 | break; |
| 3451 | case AF_INET6: |
| 3452 | 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] | 3453 | ((struct sockaddr_in6 *)dest)->sin6_port = prev_port; |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3454 | break; |
| 3455 | } |
| 3456 | |
| 3457 | return dest; |
| 3458 | } |
| 3459 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3460 | char *human_time(int t, short hz_div) { |
| 3461 | static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s" |
| 3462 | char *p = rv; |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3463 | char *end = rv + sizeof(rv); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3464 | int cnt=2; // print two numbers |
| 3465 | |
| 3466 | if (unlikely(t < 0 || hz_div <= 0)) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3467 | snprintf(p, end - p, "?"); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3468 | return rv; |
| 3469 | } |
| 3470 | |
| 3471 | if (unlikely(hz_div > 1)) |
| 3472 | t /= hz_div; |
| 3473 | |
| 3474 | if (t >= DAY) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3475 | p += snprintf(p, end - p, "%dd", t / DAY); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3476 | cnt--; |
| 3477 | } |
| 3478 | |
| 3479 | if (cnt && t % DAY / HOUR) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3480 | p += snprintf(p, end - p, "%dh", t % DAY / HOUR); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3481 | cnt--; |
| 3482 | } |
| 3483 | |
| 3484 | if (cnt && t % HOUR / MINUTE) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3485 | p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3486 | cnt--; |
| 3487 | } |
| 3488 | |
| 3489 | if ((cnt && t % MINUTE) || !t) // also display '0s' |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3490 | p += snprintf(p, end - p, "%ds", t % MINUTE / SEC); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3491 | |
| 3492 | return rv; |
| 3493 | } |
| 3494 | |
| 3495 | const char *monthname[12] = { |
| 3496 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 3497 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 3498 | }; |
| 3499 | |
| 3500 | /* date2str_log: write a date in the format : |
| 3501 | * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d", |
| 3502 | * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 3503 | * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000); |
| 3504 | * |
| 3505 | * without using sprintf. return a pointer to the last char written (\0) or |
| 3506 | * NULL if there isn't enough space. |
| 3507 | */ |
Willy Tarreau | f16cb41 | 2018-09-04 19:08:48 +0200 | [diff] [blame] | 3508 | 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] | 3509 | { |
| 3510 | |
| 3511 | if (size < 25) /* the size is fixed: 24 chars + \0 */ |
| 3512 | return NULL; |
| 3513 | |
| 3514 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3515 | if (!dst) |
| 3516 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3517 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3518 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3519 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 3520 | dst += 3; |
| 3521 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3522 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3523 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3524 | if (!dst) |
| 3525 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3526 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3527 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3528 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3529 | if (!dst) |
| 3530 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3531 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3532 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3533 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3534 | if (!dst) |
| 3535 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3536 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3537 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3538 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3539 | if (!dst) |
| 3540 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3541 | *dst++ = '.'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3542 | |
Willy Tarreau | 7d9421d | 2020-02-29 09:08:02 +0100 | [diff] [blame] | 3543 | 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] | 3544 | if (!dst) |
| 3545 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3546 | *dst = '\0'; |
| 3547 | |
| 3548 | return dst; |
| 3549 | } |
| 3550 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3551 | /* Base year used to compute leap years */ |
| 3552 | #define TM_YEAR_BASE 1900 |
| 3553 | |
| 3554 | /* Return the difference in seconds between two times (leap seconds are ignored). |
| 3555 | * Retrieved from glibc 2.18 source code. |
| 3556 | */ |
| 3557 | static int my_tm_diff(const struct tm *a, const struct tm *b) |
| 3558 | { |
| 3559 | /* Compute intervening leap days correctly even if year is negative. |
| 3560 | * Take care to avoid int overflow in leap day calculations, |
| 3561 | * but it's OK to assume that A and B are close to each other. |
| 3562 | */ |
| 3563 | int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3); |
| 3564 | int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3); |
| 3565 | int a100 = a4 / 25 - (a4 % 25 < 0); |
| 3566 | int b100 = b4 / 25 - (b4 % 25 < 0); |
| 3567 | int a400 = a100 >> 2; |
| 3568 | int b400 = b100 >> 2; |
| 3569 | int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); |
| 3570 | int years = a->tm_year - b->tm_year; |
| 3571 | int days = (365 * years + intervening_leap_days |
| 3572 | + (a->tm_yday - b->tm_yday)); |
| 3573 | return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour)) |
| 3574 | + (a->tm_min - b->tm_min)) |
| 3575 | + (a->tm_sec - b->tm_sec)); |
| 3576 | } |
| 3577 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3578 | /* Return the GMT offset for a specific local time. |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3579 | * Both t and tm must represent the same time. |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3580 | * The string returned has the same format as returned by strftime(... "%z", tm). |
| 3581 | * Offsets are kept in an internal cache for better performances. |
| 3582 | */ |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3583 | const char *get_gmt_offset(time_t t, struct tm *tm) |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3584 | { |
| 3585 | /* Cache offsets from GMT (depending on whether DST is active or not) */ |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 3586 | static THREAD_LOCAL char gmt_offsets[2][5+1] = { "", "" }; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3587 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3588 | char *gmt_offset; |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3589 | struct tm tm_gmt; |
| 3590 | int diff; |
| 3591 | int isdst = tm->tm_isdst; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3592 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3593 | /* Pretend DST not active if its status is unknown */ |
| 3594 | if (isdst < 0) |
| 3595 | isdst = 0; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3596 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3597 | /* Fetch the offset and initialize it if needed */ |
| 3598 | gmt_offset = gmt_offsets[isdst & 0x01]; |
| 3599 | if (unlikely(!*gmt_offset)) { |
| 3600 | get_gmtime(t, &tm_gmt); |
| 3601 | diff = my_tm_diff(tm, &tm_gmt); |
| 3602 | if (diff < 0) { |
| 3603 | diff = -diff; |
| 3604 | *gmt_offset = '-'; |
| 3605 | } else { |
| 3606 | *gmt_offset = '+'; |
| 3607 | } |
Willy Tarreau | e112c8a | 2019-10-29 10:16:11 +0100 | [diff] [blame] | 3608 | diff %= 86400U; |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3609 | diff /= 60; /* Convert to minutes */ |
| 3610 | snprintf(gmt_offset+1, 4+1, "%02d%02d", diff/60, diff%60); |
| 3611 | } |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3612 | |
Willy Tarreau | e112c8a | 2019-10-29 10:16:11 +0100 | [diff] [blame] | 3613 | return gmt_offset; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3614 | } |
| 3615 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3616 | /* gmt2str_log: write a date in the format : |
| 3617 | * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf |
| 3618 | * return a pointer to the last char written (\0) or |
| 3619 | * NULL if there isn't enough space. |
| 3620 | */ |
| 3621 | char *gmt2str_log(char *dst, struct tm *tm, size_t size) |
| 3622 | { |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3623 | if (size < 27) /* the size is fixed: 26 chars + \0 */ |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3624 | return NULL; |
| 3625 | |
| 3626 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3627 | if (!dst) |
| 3628 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3629 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3630 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3631 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 3632 | dst += 3; |
| 3633 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3634 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3635 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3636 | if (!dst) |
| 3637 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3638 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3639 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3640 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3641 | if (!dst) |
| 3642 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3643 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3644 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3645 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3646 | if (!dst) |
| 3647 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3648 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3649 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3650 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3651 | if (!dst) |
| 3652 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3653 | *dst++ = ' '; |
| 3654 | *dst++ = '+'; |
| 3655 | *dst++ = '0'; |
| 3656 | *dst++ = '0'; |
| 3657 | *dst++ = '0'; |
| 3658 | *dst++ = '0'; |
| 3659 | *dst = '\0'; |
| 3660 | |
| 3661 | return dst; |
| 3662 | } |
| 3663 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3664 | /* localdate2str_log: write a date in the format : |
| 3665 | * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3666 | * Both t and tm must represent the same time. |
| 3667 | * return a pointer to the last char written (\0) or |
| 3668 | * NULL if there isn't enough space. |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3669 | */ |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3670 | 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] | 3671 | { |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3672 | const char *gmt_offset; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3673 | if (size < 27) /* the size is fixed: 26 chars + \0 */ |
| 3674 | return NULL; |
| 3675 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3676 | gmt_offset = get_gmt_offset(t, tm); |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3677 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3678 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3679 | if (!dst) |
| 3680 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3681 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3682 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3683 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 3684 | dst += 3; |
| 3685 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3686 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3687 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3688 | if (!dst) |
| 3689 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3690 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3691 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3692 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3693 | if (!dst) |
| 3694 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3695 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3696 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3697 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3698 | if (!dst) |
| 3699 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3700 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3701 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3702 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3703 | if (!dst) |
| 3704 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3705 | *dst++ = ' '; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3706 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3707 | memcpy(dst, gmt_offset, 5); // Offset from local time to GMT |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3708 | dst += 5; |
| 3709 | *dst = '\0'; |
| 3710 | |
| 3711 | return dst; |
| 3712 | } |
| 3713 | |
Willy Tarreau | cb1949b | 2017-07-19 19:05:29 +0200 | [diff] [blame] | 3714 | /* Returns the number of seconds since 01/01/1970 0:0:0 GMT for GMT date <tm>. |
| 3715 | * It is meant as a portable replacement for timegm() for use with valid inputs. |
| 3716 | * Returns undefined results for invalid dates (eg: months out of range 0..11). |
| 3717 | */ |
| 3718 | time_t my_timegm(const struct tm *tm) |
| 3719 | { |
| 3720 | /* Each month has 28, 29, 30 or 31 days, or 28+N. The date in the year |
| 3721 | * is thus (current month - 1)*28 + cumulated_N[month] to count the |
| 3722 | * sum of the extra N days for elapsed months. The sum of all these N |
| 3723 | * days doesn't exceed 30 for a complete year (366-12*28) so it fits |
| 3724 | * in a 5-bit word. This means that with 60 bits we can represent a |
| 3725 | * matrix of all these values at once, which is fast and efficient to |
| 3726 | * access. The extra February day for leap years is not counted here. |
| 3727 | * |
| 3728 | * Jan : none = 0 (0) |
| 3729 | * Feb : Jan = 3 (3) |
| 3730 | * Mar : Jan..Feb = 3 (3 + 0) |
| 3731 | * Apr : Jan..Mar = 6 (3 + 0 + 3) |
| 3732 | * May : Jan..Apr = 8 (3 + 0 + 3 + 2) |
| 3733 | * Jun : Jan..May = 11 (3 + 0 + 3 + 2 + 3) |
| 3734 | * Jul : Jan..Jun = 13 (3 + 0 + 3 + 2 + 3 + 2) |
| 3735 | * Aug : Jan..Jul = 16 (3 + 0 + 3 + 2 + 3 + 2 + 3) |
| 3736 | * Sep : Jan..Aug = 19 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3) |
| 3737 | * Oct : Jan..Sep = 21 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3 + 2) |
| 3738 | * Nov : Jan..Oct = 24 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3 + 2 + 3) |
| 3739 | * Dec : Jan..Nov = 26 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3 + 2 + 3 + 2) |
| 3740 | */ |
| 3741 | uint64_t extra = |
| 3742 | ( 0ULL << 0*5) + ( 3ULL << 1*5) + ( 3ULL << 2*5) + /* Jan, Feb, Mar, */ |
| 3743 | ( 6ULL << 3*5) + ( 8ULL << 4*5) + (11ULL << 5*5) + /* Apr, May, Jun, */ |
| 3744 | (13ULL << 6*5) + (16ULL << 7*5) + (19ULL << 8*5) + /* Jul, Aug, Sep, */ |
| 3745 | (21ULL << 9*5) + (24ULL << 10*5) + (26ULL << 11*5); /* Oct, Nov, Dec, */ |
| 3746 | |
| 3747 | unsigned int y = tm->tm_year + 1900; |
| 3748 | unsigned int m = tm->tm_mon; |
| 3749 | unsigned long days = 0; |
| 3750 | |
| 3751 | /* days since 1/1/1970 for full years */ |
| 3752 | days += days_since_zero(y) - days_since_zero(1970); |
| 3753 | |
| 3754 | /* days for full months in the current year */ |
| 3755 | days += 28 * m + ((extra >> (m * 5)) & 0x1f); |
| 3756 | |
| 3757 | /* count + 1 after March for leap years. A leap year is a year multiple |
| 3758 | * of 4, unless it's multiple of 100 without being multiple of 400. 2000 |
| 3759 | * is leap, 1900 isn't, 1904 is. |
| 3760 | */ |
| 3761 | if ((m > 1) && !(y & 3) && ((y % 100) || !(y % 400))) |
| 3762 | days++; |
| 3763 | |
| 3764 | days += tm->tm_mday - 1; |
| 3765 | return days * 86400ULL + tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec; |
| 3766 | } |
| 3767 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3768 | /* This function check a char. It returns true and updates |
| 3769 | * <date> and <len> pointer to the new position if the |
| 3770 | * character is found. |
| 3771 | */ |
| 3772 | static inline int parse_expect_char(const char **date, int *len, char c) |
| 3773 | { |
| 3774 | if (*len < 1 || **date != c) |
| 3775 | return 0; |
| 3776 | (*len)--; |
| 3777 | (*date)++; |
| 3778 | return 1; |
| 3779 | } |
| 3780 | |
| 3781 | /* This function expects a string <str> of len <l>. It return true and updates. |
| 3782 | * <date> and <len> if the string matches, otherwise, it returns false. |
| 3783 | */ |
| 3784 | static inline int parse_strcmp(const char **date, int *len, char *str, int l) |
| 3785 | { |
| 3786 | if (*len < l || strncmp(*date, str, l) != 0) |
| 3787 | return 0; |
| 3788 | (*len) -= l; |
| 3789 | (*date) += l; |
| 3790 | return 1; |
| 3791 | } |
| 3792 | |
| 3793 | /* This macro converts 3 chars name in integer. */ |
| 3794 | #define STR2I3(__a, __b, __c) ((__a) * 65536 + (__b) * 256 + (__c)) |
| 3795 | |
| 3796 | /* day-name = %x4D.6F.6E ; "Mon", case-sensitive |
| 3797 | * / %x54.75.65 ; "Tue", case-sensitive |
| 3798 | * / %x57.65.64 ; "Wed", case-sensitive |
| 3799 | * / %x54.68.75 ; "Thu", case-sensitive |
| 3800 | * / %x46.72.69 ; "Fri", case-sensitive |
| 3801 | * / %x53.61.74 ; "Sat", case-sensitive |
| 3802 | * / %x53.75.6E ; "Sun", case-sensitive |
| 3803 | * |
| 3804 | * This array must be alphabetically sorted |
| 3805 | */ |
| 3806 | static inline int parse_http_dayname(const char **date, int *len, struct tm *tm) |
| 3807 | { |
| 3808 | if (*len < 3) |
| 3809 | return 0; |
| 3810 | switch (STR2I3((*date)[0], (*date)[1], (*date)[2])) { |
| 3811 | case STR2I3('M','o','n'): tm->tm_wday = 1; break; |
| 3812 | case STR2I3('T','u','e'): tm->tm_wday = 2; break; |
| 3813 | case STR2I3('W','e','d'): tm->tm_wday = 3; break; |
| 3814 | case STR2I3('T','h','u'): tm->tm_wday = 4; break; |
| 3815 | case STR2I3('F','r','i'): tm->tm_wday = 5; break; |
| 3816 | case STR2I3('S','a','t'): tm->tm_wday = 6; break; |
| 3817 | case STR2I3('S','u','n'): tm->tm_wday = 7; break; |
| 3818 | default: return 0; |
| 3819 | } |
| 3820 | *len -= 3; |
| 3821 | *date += 3; |
| 3822 | return 1; |
| 3823 | } |
| 3824 | |
| 3825 | /* month = %x4A.61.6E ; "Jan", case-sensitive |
| 3826 | * / %x46.65.62 ; "Feb", case-sensitive |
| 3827 | * / %x4D.61.72 ; "Mar", case-sensitive |
| 3828 | * / %x41.70.72 ; "Apr", case-sensitive |
| 3829 | * / %x4D.61.79 ; "May", case-sensitive |
| 3830 | * / %x4A.75.6E ; "Jun", case-sensitive |
| 3831 | * / %x4A.75.6C ; "Jul", case-sensitive |
| 3832 | * / %x41.75.67 ; "Aug", case-sensitive |
| 3833 | * / %x53.65.70 ; "Sep", case-sensitive |
| 3834 | * / %x4F.63.74 ; "Oct", case-sensitive |
| 3835 | * / %x4E.6F.76 ; "Nov", case-sensitive |
| 3836 | * / %x44.65.63 ; "Dec", case-sensitive |
| 3837 | * |
| 3838 | * This array must be alphabetically sorted |
| 3839 | */ |
| 3840 | static inline int parse_http_monthname(const char **date, int *len, struct tm *tm) |
| 3841 | { |
| 3842 | if (*len < 3) |
| 3843 | return 0; |
| 3844 | switch (STR2I3((*date)[0], (*date)[1], (*date)[2])) { |
| 3845 | case STR2I3('J','a','n'): tm->tm_mon = 0; break; |
| 3846 | case STR2I3('F','e','b'): tm->tm_mon = 1; break; |
| 3847 | case STR2I3('M','a','r'): tm->tm_mon = 2; break; |
| 3848 | case STR2I3('A','p','r'): tm->tm_mon = 3; break; |
| 3849 | case STR2I3('M','a','y'): tm->tm_mon = 4; break; |
| 3850 | case STR2I3('J','u','n'): tm->tm_mon = 5; break; |
| 3851 | case STR2I3('J','u','l'): tm->tm_mon = 6; break; |
| 3852 | case STR2I3('A','u','g'): tm->tm_mon = 7; break; |
| 3853 | case STR2I3('S','e','p'): tm->tm_mon = 8; break; |
| 3854 | case STR2I3('O','c','t'): tm->tm_mon = 9; break; |
| 3855 | case STR2I3('N','o','v'): tm->tm_mon = 10; break; |
| 3856 | case STR2I3('D','e','c'): tm->tm_mon = 11; break; |
| 3857 | default: return 0; |
| 3858 | } |
| 3859 | *len -= 3; |
| 3860 | *date += 3; |
| 3861 | return 1; |
| 3862 | } |
| 3863 | |
| 3864 | /* day-name-l = %x4D.6F.6E.64.61.79 ; "Monday", case-sensitive |
| 3865 | * / %x54.75.65.73.64.61.79 ; "Tuesday", case-sensitive |
| 3866 | * / %x57.65.64.6E.65.73.64.61.79 ; "Wednesday", case-sensitive |
| 3867 | * / %x54.68.75.72.73.64.61.79 ; "Thursday", case-sensitive |
| 3868 | * / %x46.72.69.64.61.79 ; "Friday", case-sensitive |
| 3869 | * / %x53.61.74.75.72.64.61.79 ; "Saturday", case-sensitive |
| 3870 | * / %x53.75.6E.64.61.79 ; "Sunday", case-sensitive |
| 3871 | * |
| 3872 | * This array must be alphabetically sorted |
| 3873 | */ |
| 3874 | static inline int parse_http_ldayname(const char **date, int *len, struct tm *tm) |
| 3875 | { |
| 3876 | if (*len < 6) /* Minimum length. */ |
| 3877 | return 0; |
| 3878 | switch (STR2I3((*date)[0], (*date)[1], (*date)[2])) { |
| 3879 | case STR2I3('M','o','n'): |
| 3880 | RET0_UNLESS(parse_strcmp(date, len, "Monday", 6)); |
| 3881 | tm->tm_wday = 1; |
| 3882 | return 1; |
| 3883 | case STR2I3('T','u','e'): |
| 3884 | RET0_UNLESS(parse_strcmp(date, len, "Tuesday", 7)); |
| 3885 | tm->tm_wday = 2; |
| 3886 | return 1; |
| 3887 | case STR2I3('W','e','d'): |
| 3888 | RET0_UNLESS(parse_strcmp(date, len, "Wednesday", 9)); |
| 3889 | tm->tm_wday = 3; |
| 3890 | return 1; |
| 3891 | case STR2I3('T','h','u'): |
| 3892 | RET0_UNLESS(parse_strcmp(date, len, "Thursday", 8)); |
| 3893 | tm->tm_wday = 4; |
| 3894 | return 1; |
| 3895 | case STR2I3('F','r','i'): |
| 3896 | RET0_UNLESS(parse_strcmp(date, len, "Friday", 6)); |
| 3897 | tm->tm_wday = 5; |
| 3898 | return 1; |
| 3899 | case STR2I3('S','a','t'): |
| 3900 | RET0_UNLESS(parse_strcmp(date, len, "Saturday", 8)); |
| 3901 | tm->tm_wday = 6; |
| 3902 | return 1; |
| 3903 | case STR2I3('S','u','n'): |
| 3904 | RET0_UNLESS(parse_strcmp(date, len, "Sunday", 6)); |
| 3905 | tm->tm_wday = 7; |
| 3906 | return 1; |
| 3907 | } |
| 3908 | return 0; |
| 3909 | } |
| 3910 | |
| 3911 | /* This function parses exactly 1 digit and returns the numeric value in "digit". */ |
| 3912 | static inline int parse_digit(const char **date, int *len, int *digit) |
| 3913 | { |
| 3914 | if (*len < 1 || **date < '0' || **date > '9') |
| 3915 | return 0; |
| 3916 | *digit = (**date - '0'); |
| 3917 | (*date)++; |
| 3918 | (*len)--; |
| 3919 | return 1; |
| 3920 | } |
| 3921 | |
| 3922 | /* This function parses exactly 2 digits and returns the numeric value in "digit". */ |
| 3923 | static inline int parse_2digit(const char **date, int *len, int *digit) |
| 3924 | { |
| 3925 | int value; |
| 3926 | |
| 3927 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3928 | (*digit) = value * 10; |
| 3929 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3930 | (*digit) += value; |
| 3931 | |
| 3932 | return 1; |
| 3933 | } |
| 3934 | |
| 3935 | /* This function parses exactly 4 digits and returns the numeric value in "digit". */ |
| 3936 | static inline int parse_4digit(const char **date, int *len, int *digit) |
| 3937 | { |
| 3938 | int value; |
| 3939 | |
| 3940 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3941 | (*digit) = value * 1000; |
| 3942 | |
| 3943 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3944 | (*digit) += value * 100; |
| 3945 | |
| 3946 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3947 | (*digit) += value * 10; |
| 3948 | |
| 3949 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3950 | (*digit) += value; |
| 3951 | |
| 3952 | return 1; |
| 3953 | } |
| 3954 | |
| 3955 | /* time-of-day = hour ":" minute ":" second |
| 3956 | * ; 00:00:00 - 23:59:60 (leap second) |
| 3957 | * |
| 3958 | * hour = 2DIGIT |
| 3959 | * minute = 2DIGIT |
| 3960 | * second = 2DIGIT |
| 3961 | */ |
| 3962 | static inline int parse_http_time(const char **date, int *len, struct tm *tm) |
| 3963 | { |
| 3964 | RET0_UNLESS(parse_2digit(date, len, &tm->tm_hour)); /* hour 2DIGIT */ |
| 3965 | RET0_UNLESS(parse_expect_char(date, len, ':')); /* expect ":" */ |
| 3966 | RET0_UNLESS(parse_2digit(date, len, &tm->tm_min)); /* min 2DIGIT */ |
| 3967 | RET0_UNLESS(parse_expect_char(date, len, ':')); /* expect ":" */ |
| 3968 | RET0_UNLESS(parse_2digit(date, len, &tm->tm_sec)); /* sec 2DIGIT */ |
| 3969 | return 1; |
| 3970 | } |
| 3971 | |
| 3972 | /* From RFC7231 |
| 3973 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 3974 | * |
| 3975 | * IMF-fixdate = day-name "," SP date1 SP time-of-day SP GMT |
| 3976 | * ; fixed length/zone/capitalization subset of the format |
| 3977 | * ; see Section 3.3 of [RFC5322] |
| 3978 | * |
| 3979 | * |
| 3980 | * date1 = day SP month SP year |
| 3981 | * ; e.g., 02 Jun 1982 |
| 3982 | * |
| 3983 | * day = 2DIGIT |
| 3984 | * year = 4DIGIT |
| 3985 | * |
| 3986 | * GMT = %x47.4D.54 ; "GMT", case-sensitive |
| 3987 | * |
| 3988 | * time-of-day = hour ":" minute ":" second |
| 3989 | * ; 00:00:00 - 23:59:60 (leap second) |
| 3990 | * |
| 3991 | * hour = 2DIGIT |
| 3992 | * minute = 2DIGIT |
| 3993 | * second = 2DIGIT |
| 3994 | * |
| 3995 | * DIGIT = decimal 0-9 |
| 3996 | */ |
| 3997 | int parse_imf_date(const char *date, int len, struct tm *tm) |
| 3998 | { |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3999 | /* tm_gmtoff, if present, ought to be zero'ed */ |
| 4000 | memset(tm, 0, sizeof(*tm)); |
| 4001 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 4002 | RET0_UNLESS(parse_http_dayname(&date, &len, tm)); /* day-name */ |
| 4003 | RET0_UNLESS(parse_expect_char(&date, &len, ',')); /* expect "," */ |
| 4004 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4005 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_mday)); /* day 2DIGIT */ |
| 4006 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4007 | RET0_UNLESS(parse_http_monthname(&date, &len, tm)); /* Month */ |
| 4008 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4009 | RET0_UNLESS(parse_4digit(&date, &len, &tm->tm_year)); /* year = 4DIGIT */ |
| 4010 | tm->tm_year -= 1900; |
| 4011 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4012 | RET0_UNLESS(parse_http_time(&date, &len, tm)); /* Parse time. */ |
| 4013 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4014 | RET0_UNLESS(parse_strcmp(&date, &len, "GMT", 3)); /* GMT = %x47.4D.54 ; "GMT", case-sensitive */ |
| 4015 | tm->tm_isdst = -1; |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 4016 | return 1; |
| 4017 | } |
| 4018 | |
| 4019 | /* From RFC7231 |
| 4020 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 4021 | * |
| 4022 | * rfc850-date = day-name-l "," SP date2 SP time-of-day SP GMT |
| 4023 | * date2 = day "-" month "-" 2DIGIT |
| 4024 | * ; e.g., 02-Jun-82 |
| 4025 | * |
| 4026 | * day = 2DIGIT |
| 4027 | */ |
| 4028 | int parse_rfc850_date(const char *date, int len, struct tm *tm) |
| 4029 | { |
| 4030 | int year; |
| 4031 | |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 4032 | /* tm_gmtoff, if present, ought to be zero'ed */ |
| 4033 | memset(tm, 0, sizeof(*tm)); |
| 4034 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 4035 | RET0_UNLESS(parse_http_ldayname(&date, &len, tm)); /* Read the day name */ |
| 4036 | RET0_UNLESS(parse_expect_char(&date, &len, ',')); /* expect "," */ |
| 4037 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4038 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_mday)); /* day 2DIGIT */ |
| 4039 | RET0_UNLESS(parse_expect_char(&date, &len, '-')); /* expect "-" */ |
| 4040 | RET0_UNLESS(parse_http_monthname(&date, &len, tm)); /* Month */ |
| 4041 | RET0_UNLESS(parse_expect_char(&date, &len, '-')); /* expect "-" */ |
| 4042 | |
| 4043 | /* year = 2DIGIT |
| 4044 | * |
| 4045 | * Recipients of a timestamp value in rfc850-(*date) format, which uses a |
| 4046 | * two-digit year, MUST interpret a timestamp that appears to be more |
| 4047 | * than 50 years in the future as representing the most recent year in |
| 4048 | * the past that had the same last two digits. |
| 4049 | */ |
| 4050 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_year)); |
| 4051 | |
| 4052 | /* expect SP */ |
| 4053 | if (!parse_expect_char(&date, &len, ' ')) { |
| 4054 | /* Maybe we have the date with 4 digits. */ |
| 4055 | RET0_UNLESS(parse_2digit(&date, &len, &year)); |
| 4056 | tm->tm_year = (tm->tm_year * 100 + year) - 1900; |
| 4057 | /* expect SP */ |
| 4058 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); |
| 4059 | } else { |
| 4060 | /* I fix 60 as pivot: >60: +1900, <60: +2000. Note that the |
| 4061 | * tm_year is the number of year since 1900, so for +1900, we |
| 4062 | * do nothing, and for +2000, we add 100. |
| 4063 | */ |
| 4064 | if (tm->tm_year <= 60) |
| 4065 | tm->tm_year += 100; |
| 4066 | } |
| 4067 | |
| 4068 | RET0_UNLESS(parse_http_time(&date, &len, tm)); /* Parse time. */ |
| 4069 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4070 | RET0_UNLESS(parse_strcmp(&date, &len, "GMT", 3)); /* GMT = %x47.4D.54 ; "GMT", case-sensitive */ |
| 4071 | tm->tm_isdst = -1; |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 4072 | |
| 4073 | return 1; |
| 4074 | } |
| 4075 | |
| 4076 | /* From RFC7231 |
| 4077 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 4078 | * |
| 4079 | * asctime-date = day-name SP date3 SP time-of-day SP year |
| 4080 | * date3 = month SP ( 2DIGIT / ( SP 1DIGIT )) |
| 4081 | * ; e.g., Jun 2 |
| 4082 | * |
| 4083 | * HTTP-date is case sensitive. A sender MUST NOT generate additional |
| 4084 | * whitespace in an HTTP-date beyond that specifically included as SP in |
| 4085 | * the grammar. |
| 4086 | */ |
| 4087 | int parse_asctime_date(const char *date, int len, struct tm *tm) |
| 4088 | { |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 4089 | /* tm_gmtoff, if present, ought to be zero'ed */ |
| 4090 | memset(tm, 0, sizeof(*tm)); |
| 4091 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 4092 | RET0_UNLESS(parse_http_dayname(&date, &len, tm)); /* day-name */ |
| 4093 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4094 | RET0_UNLESS(parse_http_monthname(&date, &len, tm)); /* expect month */ |
| 4095 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4096 | |
| 4097 | /* expect SP and 1DIGIT or 2DIGIT */ |
| 4098 | if (parse_expect_char(&date, &len, ' ')) |
| 4099 | RET0_UNLESS(parse_digit(&date, &len, &tm->tm_mday)); |
| 4100 | else |
| 4101 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_mday)); |
| 4102 | |
| 4103 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4104 | RET0_UNLESS(parse_http_time(&date, &len, tm)); /* Parse time. */ |
| 4105 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4106 | RET0_UNLESS(parse_4digit(&date, &len, &tm->tm_year)); /* year = 4DIGIT */ |
| 4107 | tm->tm_year -= 1900; |
| 4108 | tm->tm_isdst = -1; |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 4109 | return 1; |
| 4110 | } |
| 4111 | |
| 4112 | /* From RFC7231 |
| 4113 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 4114 | * |
| 4115 | * HTTP-date = IMF-fixdate / obs-date |
| 4116 | * obs-date = rfc850-date / asctime-date |
| 4117 | * |
| 4118 | * parses an HTTP date in the RFC format and is accepted |
| 4119 | * alternatives. <date> is the strinf containing the date, |
| 4120 | * len is the len of the string. <tm> is filled with the |
| 4121 | * parsed time. We must considers this time as GMT. |
| 4122 | */ |
| 4123 | int parse_http_date(const char *date, int len, struct tm *tm) |
| 4124 | { |
| 4125 | if (parse_imf_date(date, len, tm)) |
| 4126 | return 1; |
| 4127 | |
| 4128 | if (parse_rfc850_date(date, len, tm)) |
| 4129 | return 1; |
| 4130 | |
| 4131 | if (parse_asctime_date(date, len, tm)) |
| 4132 | return 1; |
| 4133 | |
| 4134 | return 0; |
| 4135 | } |
| 4136 | |
Willy Tarreau | 4deeb10 | 2021-01-29 10:47:52 +0100 | [diff] [blame] | 4137 | /* print the time <ns> in a short form (exactly 7 chars) at the end of buffer |
| 4138 | * <out>. "-" is printed if the value is zero, "inf" if larger than 1000 years. |
| 4139 | * It returns the new buffer length, or 0 if it doesn't fit. The value will be |
| 4140 | * surrounded by <pfx> and <sfx> respectively if not NULL. |
| 4141 | */ |
| 4142 | int print_time_short(struct buffer *out, const char *pfx, uint64_t ns, const char *sfx) |
| 4143 | { |
| 4144 | double val = ns; // 52 bits of mantissa keep ns accuracy over 52 days |
| 4145 | const char *unit; |
| 4146 | |
| 4147 | if (!pfx) |
| 4148 | pfx = ""; |
| 4149 | if (!sfx) |
| 4150 | sfx = ""; |
| 4151 | |
| 4152 | do { |
| 4153 | unit = " - "; if (val <= 0.0) break; |
| 4154 | unit = "ns"; if (val < 1000.0) break; |
| 4155 | unit = "us"; val /= 1000.0; if (val < 1000.0) break; |
| 4156 | unit = "ms"; val /= 1000.0; if (val < 1000.0) break; |
| 4157 | unit = "s "; val /= 1000.0; if (val < 60.0) break; |
| 4158 | unit = "m "; val /= 60.0; if (val < 60.0) break; |
| 4159 | unit = "h "; val /= 60.0; if (val < 24.0) break; |
| 4160 | unit = "d "; val /= 24.0; if (val < 365.0) break; |
| 4161 | unit = "yr"; val /= 365.0; if (val < 1000.0) break; |
| 4162 | unit = " inf "; val = 0.0; break; |
| 4163 | } while (0); |
| 4164 | |
| 4165 | if (val <= 0.0) |
| 4166 | return chunk_appendf(out, "%s%7s%s", pfx, unit, sfx); |
| 4167 | else if (val < 10.0) |
| 4168 | return chunk_appendf(out, "%s%1.3f%s%s", pfx, val, unit, sfx); |
| 4169 | else if (val < 100.0) |
| 4170 | return chunk_appendf(out, "%s%2.2f%s%s", pfx, val, unit, sfx); |
| 4171 | else |
| 4172 | return chunk_appendf(out, "%s%3.1f%s%s", pfx, val, unit, sfx); |
| 4173 | } |
| 4174 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4175 | /* Dynamically allocates a string of the proper length to hold the formatted |
| 4176 | * output. NULL is returned on error. The caller is responsible for freeing the |
| 4177 | * memory area using free(). The resulting string is returned in <out> if the |
| 4178 | * pointer is not NULL. A previous version of <out> might be used to build the |
| 4179 | * new string, and it will be freed before returning if it is not NULL, which |
| 4180 | * makes it possible to build complex strings from iterative calls without |
| 4181 | * having to care about freeing intermediate values, as in the example below : |
| 4182 | * |
| 4183 | * memprintf(&err, "invalid argument: '%s'", arg); |
| 4184 | * ... |
| 4185 | * memprintf(&err, "parser said : <%s>\n", *err); |
| 4186 | * ... |
| 4187 | * free(*err); |
| 4188 | * |
| 4189 | * This means that <err> must be initialized to NULL before first invocation. |
| 4190 | * The return value also holds the allocated string, which eases error checking |
| 4191 | * 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] | 4192 | * passed instead and it will be ignored. The returned message will then also |
| 4193 | * 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] | 4194 | * |
| 4195 | * It is also convenient to use it without any free except the last one : |
| 4196 | * err = NULL; |
| 4197 | * if (!fct1(err)) report(*err); |
| 4198 | * if (!fct2(err)) report(*err); |
| 4199 | * if (!fct3(err)) report(*err); |
| 4200 | * free(*err); |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 4201 | * |
| 4202 | * memprintf relies on memvprintf. This last version can be called from any |
| 4203 | * function with variadic arguments. |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4204 | */ |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 4205 | char *memvprintf(char **out, const char *format, va_list orig_args) |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4206 | { |
| 4207 | va_list args; |
| 4208 | char *ret = NULL; |
| 4209 | int allocated = 0; |
| 4210 | int needed = 0; |
| 4211 | |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 4212 | if (!out) |
| 4213 | return NULL; |
| 4214 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4215 | do { |
Willy Tarreau | e0609f5 | 2019-03-29 19:13:23 +0100 | [diff] [blame] | 4216 | char buf1; |
| 4217 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4218 | /* vsnprintf() will return the required length even when the |
| 4219 | * target buffer is NULL. We do this in a loop just in case |
| 4220 | * intermediate evaluations get wrong. |
| 4221 | */ |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 4222 | va_copy(args, orig_args); |
Willy Tarreau | e0609f5 | 2019-03-29 19:13:23 +0100 | [diff] [blame] | 4223 | needed = vsnprintf(ret ? ret : &buf1, allocated, format, args); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4224 | va_end(args); |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 4225 | if (needed < allocated) { |
| 4226 | /* Note: on Solaris 8, the first iteration always |
| 4227 | * returns -1 if allocated is zero, so we force a |
| 4228 | * retry. |
| 4229 | */ |
| 4230 | if (!allocated) |
| 4231 | needed = 0; |
| 4232 | else |
| 4233 | break; |
| 4234 | } |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4235 | |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 4236 | allocated = needed + 1; |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 4237 | ret = my_realloc2(ret, allocated); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4238 | } while (ret); |
| 4239 | |
| 4240 | if (needed < 0) { |
| 4241 | /* an error was encountered */ |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4242 | ha_free(&ret); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4243 | } |
| 4244 | |
| 4245 | if (out) { |
| 4246 | free(*out); |
| 4247 | *out = ret; |
| 4248 | } |
| 4249 | |
| 4250 | return ret; |
| 4251 | } |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 4252 | |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 4253 | char *memprintf(char **out, const char *format, ...) |
| 4254 | { |
| 4255 | va_list args; |
| 4256 | char *ret = NULL; |
| 4257 | |
| 4258 | va_start(args, format); |
| 4259 | ret = memvprintf(out, format, args); |
| 4260 | va_end(args); |
| 4261 | |
| 4262 | return ret; |
| 4263 | } |
| 4264 | |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 4265 | /* Used to add <level> spaces before each line of <out>, unless there is only one line. |
| 4266 | * 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] | 4267 | * freed by the caller. It also supports being passed a NULL which results in the same |
| 4268 | * output. |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 4269 | * Example of use : |
| 4270 | * parse(cmd, &err); (callee: memprintf(&err, ...)) |
| 4271 | * fprintf(stderr, "Parser said: %s\n", indent_error(&err)); |
| 4272 | * free(err); |
| 4273 | */ |
| 4274 | char *indent_msg(char **out, int level) |
| 4275 | { |
| 4276 | char *ret, *in, *p; |
| 4277 | int needed = 0; |
| 4278 | int lf = 0; |
| 4279 | int lastlf = 0; |
| 4280 | int len; |
| 4281 | |
Willy Tarreau | 70eec38 | 2012-10-10 08:56:47 +0200 | [diff] [blame] | 4282 | if (!out || !*out) |
| 4283 | return NULL; |
| 4284 | |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 4285 | in = *out - 1; |
| 4286 | while ((in = strchr(in + 1, '\n')) != NULL) { |
| 4287 | lastlf = in - *out; |
| 4288 | lf++; |
| 4289 | } |
| 4290 | |
| 4291 | if (!lf) /* single line, no LF, return it as-is */ |
| 4292 | return *out; |
| 4293 | |
| 4294 | len = strlen(*out); |
| 4295 | |
| 4296 | if (lf == 1 && lastlf == len - 1) { |
| 4297 | /* single line, LF at end, strip it and return as-is */ |
| 4298 | (*out)[lastlf] = 0; |
| 4299 | return *out; |
| 4300 | } |
| 4301 | |
| 4302 | /* OK now we have at least one LF, we need to process the whole string |
| 4303 | * as a multi-line string. What we'll do : |
| 4304 | * - prefix with an LF if there is none |
| 4305 | * - add <level> spaces before each line |
| 4306 | * This means at most ( 1 + level + (len-lf) + lf*<1+level) ) = |
| 4307 | * 1 + level + len + lf * level = 1 + level * (lf + 1) + len. |
| 4308 | */ |
| 4309 | |
| 4310 | needed = 1 + level * (lf + 1) + len + 1; |
| 4311 | p = ret = malloc(needed); |
| 4312 | in = *out; |
| 4313 | |
| 4314 | /* skip initial LFs */ |
| 4315 | while (*in == '\n') |
| 4316 | in++; |
| 4317 | |
| 4318 | /* copy each line, prefixed with LF and <level> spaces, and without the trailing LF */ |
| 4319 | while (*in) { |
| 4320 | *p++ = '\n'; |
| 4321 | memset(p, ' ', level); |
| 4322 | p += level; |
| 4323 | do { |
| 4324 | *p++ = *in++; |
| 4325 | } while (*in && *in != '\n'); |
| 4326 | if (*in) |
| 4327 | in++; |
| 4328 | } |
| 4329 | *p = 0; |
| 4330 | |
| 4331 | free(*out); |
| 4332 | *out = ret; |
| 4333 | |
| 4334 | return ret; |
| 4335 | } |
| 4336 | |
Willy Tarreau | a2c9911 | 2019-08-21 13:17:37 +0200 | [diff] [blame] | 4337 | /* makes a copy of message <in> into <out>, with each line prefixed with <pfx> |
| 4338 | * and end of lines replaced with <eol> if not 0. The first line to indent has |
| 4339 | * to be indicated in <first> (starts at zero), so that it is possible to skip |
| 4340 | * indenting the first line if it has to be appended after an existing message. |
| 4341 | * Empty strings are never indented, and NULL strings are considered empty both |
| 4342 | * for <in> and <pfx>. It returns non-zero if an EOL was appended as the last |
| 4343 | * character, non-zero otherwise. |
| 4344 | */ |
| 4345 | int append_prefixed_str(struct buffer *out, const char *in, const char *pfx, char eol, int first) |
| 4346 | { |
| 4347 | int bol, lf; |
| 4348 | int pfxlen = pfx ? strlen(pfx) : 0; |
| 4349 | |
| 4350 | if (!in) |
| 4351 | return 0; |
| 4352 | |
| 4353 | bol = 1; |
| 4354 | lf = 0; |
| 4355 | while (*in) { |
| 4356 | if (bol && pfxlen) { |
| 4357 | if (first > 0) |
| 4358 | first--; |
| 4359 | else |
| 4360 | b_putblk(out, pfx, pfxlen); |
| 4361 | bol = 0; |
| 4362 | } |
| 4363 | |
| 4364 | lf = (*in == '\n'); |
| 4365 | bol |= lf; |
| 4366 | b_putchr(out, (lf && eol) ? eol : *in); |
| 4367 | in++; |
| 4368 | } |
| 4369 | return lf; |
| 4370 | } |
| 4371 | |
Willy Tarreau | 9d22e56 | 2019-03-29 18:49:09 +0100 | [diff] [blame] | 4372 | /* removes environment variable <name> from the environment as found in |
| 4373 | * environ. This is only provided as an alternative for systems without |
| 4374 | * unsetenv() (old Solaris and AIX versions). THIS IS NOT THREAD SAFE. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4375 | * The principle is to scan environ for each occurrence of variable name |
Willy Tarreau | 9d22e56 | 2019-03-29 18:49:09 +0100 | [diff] [blame] | 4376 | * <name> and to replace the matching pointers with the last pointer of |
| 4377 | * the array (since variables are not ordered). |
| 4378 | * It always returns 0 (success). |
| 4379 | */ |
| 4380 | int my_unsetenv(const char *name) |
| 4381 | { |
| 4382 | extern char **environ; |
| 4383 | char **p = environ; |
| 4384 | int vars; |
| 4385 | int next; |
| 4386 | int len; |
| 4387 | |
| 4388 | len = strlen(name); |
| 4389 | for (vars = 0; p[vars]; vars++) |
| 4390 | ; |
| 4391 | next = 0; |
| 4392 | while (next < vars) { |
| 4393 | if (strncmp(p[next], name, len) != 0 || p[next][len] != '=') { |
| 4394 | next++; |
| 4395 | continue; |
| 4396 | } |
| 4397 | if (next < vars - 1) |
| 4398 | p[next] = p[vars - 1]; |
| 4399 | p[--vars] = NULL; |
| 4400 | } |
| 4401 | return 0; |
| 4402 | } |
| 4403 | |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 4404 | /* Convert occurrences of environment variables in the input string to their |
| 4405 | * corresponding value. A variable is identified as a series of alphanumeric |
| 4406 | * characters or underscores following a '$' sign. The <in> string must be |
| 4407 | * free()able. NULL returns NULL. The resulting string might be reallocated if |
| 4408 | * some expansion is made. Variable names may also be enclosed into braces if |
| 4409 | * needed (eg: to concatenate alphanum characters). |
| 4410 | */ |
| 4411 | char *env_expand(char *in) |
| 4412 | { |
| 4413 | char *txt_beg; |
| 4414 | char *out; |
| 4415 | char *txt_end; |
| 4416 | char *var_beg; |
| 4417 | char *var_end; |
| 4418 | char *value; |
| 4419 | char *next; |
| 4420 | int out_len; |
| 4421 | int val_len; |
| 4422 | |
| 4423 | if (!in) |
| 4424 | return in; |
| 4425 | |
| 4426 | value = out = NULL; |
| 4427 | out_len = 0; |
| 4428 | |
| 4429 | txt_beg = in; |
| 4430 | do { |
| 4431 | /* look for next '$' sign in <in> */ |
| 4432 | for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++); |
| 4433 | |
| 4434 | if (!*txt_end && !out) /* end and no expansion performed */ |
| 4435 | return in; |
| 4436 | |
| 4437 | val_len = 0; |
| 4438 | next = txt_end; |
| 4439 | if (*txt_end == '$') { |
| 4440 | char save; |
| 4441 | |
| 4442 | var_beg = txt_end + 1; |
| 4443 | if (*var_beg == '{') |
| 4444 | var_beg++; |
| 4445 | |
| 4446 | var_end = var_beg; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4447 | while (isalnum((unsigned char)*var_end) || *var_end == '_') { |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 4448 | var_end++; |
| 4449 | } |
| 4450 | |
| 4451 | next = var_end; |
| 4452 | if (*var_end == '}' && (var_beg > txt_end + 1)) |
| 4453 | next++; |
| 4454 | |
| 4455 | /* get value of the variable name at this location */ |
| 4456 | save = *var_end; |
| 4457 | *var_end = '\0'; |
| 4458 | value = getenv(var_beg); |
| 4459 | *var_end = save; |
| 4460 | val_len = value ? strlen(value) : 0; |
| 4461 | } |
| 4462 | |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 4463 | 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] | 4464 | if (txt_end > txt_beg) { |
| 4465 | memcpy(out + out_len, txt_beg, txt_end - txt_beg); |
| 4466 | out_len += txt_end - txt_beg; |
| 4467 | } |
| 4468 | if (val_len) { |
| 4469 | memcpy(out + out_len, value, val_len); |
| 4470 | out_len += val_len; |
| 4471 | } |
| 4472 | out[out_len] = 0; |
| 4473 | txt_beg = next; |
| 4474 | } while (*txt_beg); |
| 4475 | |
| 4476 | /* here we know that <out> was allocated and that we don't need <in> anymore */ |
| 4477 | free(in); |
| 4478 | return out; |
| 4479 | } |
| 4480 | |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4481 | |
| 4482 | /* same as strstr() but case-insensitive and with limit length */ |
| 4483 | const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2) |
| 4484 | { |
| 4485 | char *pptr, *sptr, *start; |
Willy Tarreau | c874653 | 2014-05-28 23:05:07 +0200 | [diff] [blame] | 4486 | unsigned int slen, plen; |
| 4487 | unsigned int tmp1, tmp2; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4488 | |
| 4489 | if (str1 == NULL || len_str1 == 0) // search pattern into an empty string => search is not found |
| 4490 | return NULL; |
| 4491 | |
| 4492 | if (str2 == NULL || len_str2 == 0) // pattern is empty => every str1 match |
| 4493 | return str1; |
| 4494 | |
| 4495 | if (len_str1 < len_str2) // pattern is longer than string => search is not found |
| 4496 | return NULL; |
| 4497 | |
| 4498 | 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] | 4499 | while (toupper((unsigned char)*start) != toupper((unsigned char)*str2)) { |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4500 | start++; |
| 4501 | slen--; |
| 4502 | tmp1++; |
| 4503 | |
| 4504 | if (tmp1 >= len_str1) |
| 4505 | return NULL; |
| 4506 | |
| 4507 | /* if pattern longer than string */ |
| 4508 | if (slen < plen) |
| 4509 | return NULL; |
| 4510 | } |
| 4511 | |
| 4512 | sptr = start; |
| 4513 | pptr = (char *)str2; |
| 4514 | |
| 4515 | tmp2 = 0; |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 4516 | while (toupper((unsigned char)*sptr) == toupper((unsigned char)*pptr)) { |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4517 | sptr++; |
| 4518 | pptr++; |
| 4519 | tmp2++; |
| 4520 | |
| 4521 | if (*pptr == '\0' || tmp2 == len_str2) /* end of pattern found */ |
| 4522 | return start; |
| 4523 | if (*sptr == '\0' || tmp2 == len_str1) /* end of string found and the pattern is not fully found */ |
| 4524 | return NULL; |
| 4525 | } |
| 4526 | } |
| 4527 | return NULL; |
| 4528 | } |
| 4529 | |
Willy Tarreau | 3ff476e | 2022-03-30 10:02:56 +0200 | [diff] [blame] | 4530 | /* Returns true if s1 < s2 < s3 otherwise zero. Both s1 and s3 may be NULL and |
| 4531 | * in this case only non-null strings are compared. This allows to pass initial |
| 4532 | * values in iterators and in sort functions. |
| 4533 | */ |
| 4534 | int strordered(const char *s1, const char *s2, const char *s3) |
| 4535 | { |
| 4536 | return (!s1 || strcmp(s1, s2) < 0) && (!s3 || strcmp(s2, s3) < 0); |
| 4537 | } |
| 4538 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 4539 | /* This function read the next valid utf8 char. |
| 4540 | * <s> is the byte srray to be decode, <len> is its length. |
| 4541 | * The function returns decoded char encoded like this: |
| 4542 | * The 4 msb are the return code (UTF8_CODE_*), the 4 lsb |
| 4543 | * are the length read. The decoded character is stored in <c>. |
| 4544 | */ |
| 4545 | unsigned char utf8_next(const char *s, int len, unsigned int *c) |
| 4546 | { |
| 4547 | const unsigned char *p = (unsigned char *)s; |
| 4548 | int dec; |
| 4549 | unsigned char code = UTF8_CODE_OK; |
| 4550 | |
| 4551 | if (len < 1) |
| 4552 | return UTF8_CODE_OK; |
| 4553 | |
| 4554 | /* Check the type of UTF8 sequence |
| 4555 | * |
| 4556 | * 0... .... 0x00 <= x <= 0x7f : 1 byte: ascii char |
| 4557 | * 10.. .... 0x80 <= x <= 0xbf : invalid sequence |
| 4558 | * 110. .... 0xc0 <= x <= 0xdf : 2 bytes |
| 4559 | * 1110 .... 0xe0 <= x <= 0xef : 3 bytes |
| 4560 | * 1111 0... 0xf0 <= x <= 0xf7 : 4 bytes |
| 4561 | * 1111 10.. 0xf8 <= x <= 0xfb : 5 bytes |
| 4562 | * 1111 110. 0xfc <= x <= 0xfd : 6 bytes |
| 4563 | * 1111 111. 0xfe <= x <= 0xff : invalid sequence |
| 4564 | */ |
| 4565 | switch (*p) { |
| 4566 | case 0x00 ... 0x7f: |
| 4567 | *c = *p; |
| 4568 | return UTF8_CODE_OK | 1; |
| 4569 | |
| 4570 | case 0x80 ... 0xbf: |
| 4571 | *c = *p; |
| 4572 | return UTF8_CODE_BADSEQ | 1; |
| 4573 | |
| 4574 | case 0xc0 ... 0xdf: |
| 4575 | if (len < 2) { |
| 4576 | *c = *p; |
| 4577 | return UTF8_CODE_BADSEQ | 1; |
| 4578 | } |
| 4579 | *c = *p & 0x1f; |
| 4580 | dec = 1; |
| 4581 | break; |
| 4582 | |
| 4583 | case 0xe0 ... 0xef: |
| 4584 | if (len < 3) { |
| 4585 | *c = *p; |
| 4586 | return UTF8_CODE_BADSEQ | 1; |
| 4587 | } |
| 4588 | *c = *p & 0x0f; |
| 4589 | dec = 2; |
| 4590 | break; |
| 4591 | |
| 4592 | case 0xf0 ... 0xf7: |
| 4593 | if (len < 4) { |
| 4594 | *c = *p; |
| 4595 | return UTF8_CODE_BADSEQ | 1; |
| 4596 | } |
| 4597 | *c = *p & 0x07; |
| 4598 | dec = 3; |
| 4599 | break; |
| 4600 | |
| 4601 | case 0xf8 ... 0xfb: |
| 4602 | if (len < 5) { |
| 4603 | *c = *p; |
| 4604 | return UTF8_CODE_BADSEQ | 1; |
| 4605 | } |
| 4606 | *c = *p & 0x03; |
| 4607 | dec = 4; |
| 4608 | break; |
| 4609 | |
| 4610 | case 0xfc ... 0xfd: |
| 4611 | if (len < 6) { |
| 4612 | *c = *p; |
| 4613 | return UTF8_CODE_BADSEQ | 1; |
| 4614 | } |
| 4615 | *c = *p & 0x01; |
| 4616 | dec = 5; |
| 4617 | break; |
| 4618 | |
| 4619 | case 0xfe ... 0xff: |
| 4620 | default: |
| 4621 | *c = *p; |
| 4622 | return UTF8_CODE_BADSEQ | 1; |
| 4623 | } |
| 4624 | |
| 4625 | p++; |
| 4626 | |
| 4627 | while (dec > 0) { |
| 4628 | |
| 4629 | /* need 0x10 for the 2 first bits */ |
| 4630 | if ( ( *p & 0xc0 ) != 0x80 ) |
| 4631 | return UTF8_CODE_BADSEQ | ((p-(unsigned char *)s)&0xffff); |
| 4632 | |
| 4633 | /* add data at char */ |
| 4634 | *c = ( *c << 6 ) | ( *p & 0x3f ); |
| 4635 | |
| 4636 | dec--; |
| 4637 | p++; |
| 4638 | } |
| 4639 | |
| 4640 | /* Check ovelong encoding. |
| 4641 | * 1 byte : 5 + 6 : 11 : 0x80 ... 0x7ff |
| 4642 | * 2 bytes : 4 + 6 + 6 : 16 : 0x800 ... 0xffff |
| 4643 | * 3 bytes : 3 + 6 + 6 + 6 : 21 : 0x10000 ... 0x1fffff |
| 4644 | */ |
Thierry FOURNIER | 9e7ec08 | 2015-03-12 19:32:38 +0100 | [diff] [blame] | 4645 | if (( *c <= 0x7f && (p-(unsigned char *)s) > 1) || |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 4646 | (*c >= 0x80 && *c <= 0x7ff && (p-(unsigned char *)s) > 2) || |
| 4647 | (*c >= 0x800 && *c <= 0xffff && (p-(unsigned char *)s) > 3) || |
| 4648 | (*c >= 0x10000 && *c <= 0x1fffff && (p-(unsigned char *)s) > 4)) |
| 4649 | code |= UTF8_CODE_OVERLONG; |
| 4650 | |
| 4651 | /* Check invalid UTF8 range. */ |
| 4652 | if ((*c >= 0xd800 && *c <= 0xdfff) || |
| 4653 | (*c >= 0xfffe && *c <= 0xffff)) |
| 4654 | code |= UTF8_CODE_INVRANGE; |
| 4655 | |
| 4656 | return code | ((p-(unsigned char *)s)&0x0f); |
| 4657 | } |
| 4658 | |
Maxime de Roucy | dc88785 | 2016-05-13 23:52:54 +0200 | [diff] [blame] | 4659 | /* append a copy of string <str> (in a wordlist) at the end of the list <li> |
| 4660 | * On failure : return 0 and <err> filled with an error message. |
| 4661 | * The caller is responsible for freeing the <err> and <str> copy |
| 4662 | * memory area using free() |
| 4663 | */ |
| 4664 | int list_append_word(struct list *li, const char *str, char **err) |
| 4665 | { |
| 4666 | struct wordlist *wl; |
| 4667 | |
| 4668 | wl = calloc(1, sizeof(*wl)); |
| 4669 | if (!wl) { |
| 4670 | memprintf(err, "out of memory"); |
| 4671 | goto fail_wl; |
| 4672 | } |
| 4673 | |
| 4674 | wl->s = strdup(str); |
| 4675 | if (!wl->s) { |
| 4676 | memprintf(err, "out of memory"); |
| 4677 | goto fail_wl_s; |
| 4678 | } |
| 4679 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4680 | LIST_APPEND(li, &wl->list); |
Maxime de Roucy | dc88785 | 2016-05-13 23:52:54 +0200 | [diff] [blame] | 4681 | |
| 4682 | return 1; |
| 4683 | |
| 4684 | fail_wl_s: |
| 4685 | free(wl->s); |
| 4686 | fail_wl: |
| 4687 | free(wl); |
| 4688 | return 0; |
| 4689 | } |
| 4690 | |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4691 | /* indicates if a memory location may safely be read or not. The trick consists |
| 4692 | * in performing a harmless syscall using this location as an input and letting |
| 4693 | * the operating system report whether it's OK or not. For this we have the |
| 4694 | * stat() syscall, which will return EFAULT when the memory location supposed |
| 4695 | * to contain the file name is not readable. If it is readable it will then |
| 4696 | * either return 0 if the area contains an existing file name, or -1 with |
| 4697 | * another code. This must not be abused, and some audit systems might detect |
| 4698 | * this as abnormal activity. It's used only for unsafe dumps. |
| 4699 | */ |
| 4700 | int may_access(const void *ptr) |
| 4701 | { |
| 4702 | struct stat buf; |
| 4703 | |
| 4704 | if (stat(ptr, &buf) == 0) |
| 4705 | return 1; |
| 4706 | if (errno == EFAULT) |
| 4707 | return 0; |
| 4708 | return 1; |
| 4709 | } |
| 4710 | |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4711 | /* print a string of text buffer to <out>. The format is : |
| 4712 | * Non-printable chars \t, \n, \r and \e are * encoded in C format. |
| 4713 | * Other non-printable chars are encoded "\xHH". Space, '\', and '=' are also escaped. |
| 4714 | * Print stopped if null char or <bsize> is reached, or if no more place in the chunk. |
| 4715 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4716 | int dump_text(struct buffer *out, const char *buf, int bsize) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4717 | { |
| 4718 | unsigned char c; |
Tim Duesterhus | 18795d4 | 2021-08-29 00:58:22 +0200 | [diff] [blame] | 4719 | size_t ptr = 0; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4720 | |
Tim Duesterhus | 18795d4 | 2021-08-29 00:58:22 +0200 | [diff] [blame] | 4721 | while (ptr < bsize && buf[ptr]) { |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4722 | c = buf[ptr]; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4723 | if (isprint((unsigned char)c) && isascii((unsigned char)c) && c != '\\' && c != ' ' && c != '=') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4724 | if (out->data > out->size - 1) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4725 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4726 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4727 | } |
| 4728 | 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] | 4729 | if (out->data > out->size - 2) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4730 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4731 | out->area[out->data++] = '\\'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4732 | switch (c) { |
| 4733 | case ' ': c = ' '; break; |
| 4734 | case '\t': c = 't'; break; |
| 4735 | case '\n': c = 'n'; break; |
| 4736 | case '\r': c = 'r'; break; |
| 4737 | case '\e': c = 'e'; break; |
| 4738 | case '\\': c = '\\'; break; |
| 4739 | case '=': c = '='; break; |
| 4740 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4741 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4742 | } |
| 4743 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4744 | if (out->data > out->size - 4) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4745 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4746 | out->area[out->data++] = '\\'; |
| 4747 | out->area[out->data++] = 'x'; |
| 4748 | out->area[out->data++] = hextab[(c >> 4) & 0xF]; |
| 4749 | out->area[out->data++] = hextab[c & 0xF]; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4750 | } |
| 4751 | ptr++; |
| 4752 | } |
| 4753 | |
| 4754 | return ptr; |
| 4755 | } |
| 4756 | |
| 4757 | /* print a buffer in hexa. |
| 4758 | * Print stopped if <bsize> is reached, or if no more place in the chunk. |
| 4759 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4760 | int dump_binary(struct buffer *out, const char *buf, int bsize) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4761 | { |
| 4762 | unsigned char c; |
| 4763 | int ptr = 0; |
| 4764 | |
| 4765 | while (ptr < bsize) { |
| 4766 | c = buf[ptr]; |
| 4767 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4768 | if (out->data > out->size - 2) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4769 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4770 | out->area[out->data++] = hextab[(c >> 4) & 0xF]; |
| 4771 | out->area[out->data++] = hextab[c & 0xF]; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4772 | |
| 4773 | ptr++; |
| 4774 | } |
| 4775 | return ptr; |
| 4776 | } |
| 4777 | |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4778 | /* Appends into buffer <out> a hex dump of memory area <buf> for <len> bytes, |
| 4779 | * prepending each line with prefix <pfx>. The output is *not* initialized. |
| 4780 | * The output will not wrap pas the buffer's end so it is more optimal if the |
| 4781 | * caller makes sure the buffer is aligned first. A trailing zero will always |
| 4782 | * 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] | 4783 | * sure that the area is dumpable first. If <unsafe> is non-null, the memory |
| 4784 | * locations are checked first for being readable. |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4785 | */ |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4786 | 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] | 4787 | { |
| 4788 | const unsigned char *d = buf; |
| 4789 | int i, j, start; |
| 4790 | |
| 4791 | d = (const unsigned char *)(((unsigned long)buf) & -16); |
| 4792 | start = ((unsigned long)buf) & 15; |
| 4793 | |
| 4794 | for (i = 0; i < start + len; i += 16) { |
| 4795 | chunk_appendf(out, (sizeof(void *) == 4) ? "%s%8p: " : "%s%16p: ", pfx, d + i); |
| 4796 | |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4797 | // 0: unchecked, 1: checked safe, 2: danger |
| 4798 | unsafe = !!unsafe; |
| 4799 | if (unsafe && !may_access(d + i)) |
| 4800 | unsafe = 2; |
| 4801 | |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4802 | for (j = 0; j < 16; j++) { |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4803 | if ((i + j < start) || (i + j >= start + len)) |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4804 | chunk_strcat(out, "'' "); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4805 | else if (unsafe > 1) |
| 4806 | chunk_strcat(out, "** "); |
| 4807 | else |
| 4808 | chunk_appendf(out, "%02x ", d[i + j]); |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4809 | |
| 4810 | if (j == 7) |
| 4811 | chunk_strcat(out, "- "); |
| 4812 | } |
| 4813 | chunk_strcat(out, " "); |
| 4814 | for (j = 0; j < 16; j++) { |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4815 | if ((i + j < start) || (i + j >= start + len)) |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4816 | chunk_strcat(out, "'"); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4817 | else if (unsafe > 1) |
| 4818 | chunk_strcat(out, "*"); |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4819 | else if (isprint((unsigned char)d[i + j])) |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4820 | chunk_appendf(out, "%c", d[i + j]); |
| 4821 | else |
| 4822 | chunk_strcat(out, "."); |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4823 | } |
| 4824 | chunk_strcat(out, "\n"); |
| 4825 | } |
| 4826 | } |
| 4827 | |
Willy Tarreau | 762fb3e | 2020-03-03 15:57:10 +0100 | [diff] [blame] | 4828 | /* dumps <pfx> followed by <n> bytes from <addr> in hex form into buffer <buf> |
| 4829 | * enclosed in brackets after the address itself, formatted on 14 chars |
| 4830 | * including the "0x" prefix. This is meant to be used as a prefix for code |
| 4831 | * areas. For example: |
| 4832 | * "0x7f10b6557690 [48 c7 c0 0f 00 00 00 0f]" |
| 4833 | * It relies on may_access() to know if the bytes are dumpable, otherwise "--" |
| 4834 | * is emitted. A NULL <pfx> will be considered empty. |
| 4835 | */ |
| 4836 | void dump_addr_and_bytes(struct buffer *buf, const char *pfx, const void *addr, int n) |
| 4837 | { |
| 4838 | int ok = 0; |
| 4839 | int i; |
| 4840 | |
| 4841 | chunk_appendf(buf, "%s%#14lx [", pfx ? pfx : "", (long)addr); |
| 4842 | |
| 4843 | for (i = 0; i < n; i++) { |
| 4844 | if (i == 0 || (((long)(addr + i) ^ (long)(addr)) & 4096)) |
| 4845 | ok = may_access(addr + i); |
| 4846 | if (ok) |
| 4847 | chunk_appendf(buf, "%02x%s", ((uint8_t*)addr)[i], (i<n-1) ? " " : "]"); |
| 4848 | else |
| 4849 | chunk_appendf(buf, "--%s", (i<n-1) ? " " : "]"); |
| 4850 | } |
| 4851 | } |
| 4852 | |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4853 | /* print a line of text buffer (limited to 70 bytes) to <out>. The format is : |
| 4854 | * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n> |
| 4855 | * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are |
| 4856 | * encoded in C format. Other non-printable chars are encoded "\xHH". Original |
| 4857 | * lines are respected within the limit of 70 output chars. Lines that are |
| 4858 | * continuation of a previous truncated line begin with "+" instead of " " |
| 4859 | * after the offset. The new pointer is returned. |
| 4860 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4861 | 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] | 4862 | int *line, int ptr) |
| 4863 | { |
| 4864 | int end; |
| 4865 | unsigned char c; |
| 4866 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4867 | end = out->data + 80; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4868 | if (end > out->size) |
| 4869 | return ptr; |
| 4870 | |
| 4871 | chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+'); |
| 4872 | |
| 4873 | while (ptr < len && ptr < bsize) { |
| 4874 | c = buf[ptr]; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4875 | if (isprint((unsigned char)c) && isascii((unsigned char)c) && c != '\\') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4876 | if (out->data > end - 2) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4877 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4878 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4879 | } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4880 | if (out->data > end - 3) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4881 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4882 | out->area[out->data++] = '\\'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4883 | switch (c) { |
| 4884 | case '\t': c = 't'; break; |
| 4885 | case '\n': c = 'n'; break; |
| 4886 | case '\r': c = 'r'; break; |
| 4887 | case '\e': c = 'e'; break; |
| 4888 | case '\\': c = '\\'; break; |
| 4889 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4890 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4891 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4892 | if (out->data > end - 5) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4893 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4894 | out->area[out->data++] = '\\'; |
| 4895 | out->area[out->data++] = 'x'; |
| 4896 | out->area[out->data++] = hextab[(c >> 4) & 0xF]; |
| 4897 | out->area[out->data++] = hextab[c & 0xF]; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4898 | } |
| 4899 | if (buf[ptr++] == '\n') { |
| 4900 | /* we had a line break, let's return now */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4901 | out->area[out->data++] = '\n'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4902 | *line = ptr; |
| 4903 | return ptr; |
| 4904 | } |
| 4905 | } |
| 4906 | /* we have an incomplete line, we return it as-is */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4907 | out->area[out->data++] = '\n'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4908 | return ptr; |
| 4909 | } |
| 4910 | |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4911 | /* 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] | 4912 | * has address <baseaddr>. String <pfx> may be placed as a prefix in front of |
| 4913 | * 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] | 4914 | */ |
Willy Tarreau | ed936c5 | 2017-04-27 18:03:20 +0200 | [diff] [blame] | 4915 | void debug_hexdump(FILE *out, const char *pfx, const char *buf, |
| 4916 | unsigned int baseaddr, int len) |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4917 | { |
Willy Tarreau | 7345979 | 2017-04-11 07:58:08 +0200 | [diff] [blame] | 4918 | unsigned int i; |
| 4919 | int b, j; |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4920 | |
| 4921 | for (i = 0; i < (len + (baseaddr & 15)); i += 16) { |
| 4922 | b = i - (baseaddr & 15); |
Willy Tarreau | ed936c5 | 2017-04-27 18:03:20 +0200 | [diff] [blame] | 4923 | fprintf(out, "%s%08x: ", pfx ? pfx : "", i + (baseaddr & ~15)); |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4924 | for (j = 0; j < 8; j++) { |
| 4925 | if (b + j >= 0 && b + j < len) |
| 4926 | fprintf(out, "%02x ", (unsigned char)buf[b + j]); |
| 4927 | else |
| 4928 | fprintf(out, " "); |
| 4929 | } |
| 4930 | |
| 4931 | if (b + j >= 0 && b + j < len) |
| 4932 | fputc('-', out); |
| 4933 | else |
| 4934 | fputc(' ', out); |
| 4935 | |
| 4936 | for (j = 8; j < 16; j++) { |
| 4937 | if (b + j >= 0 && b + j < len) |
| 4938 | fprintf(out, " %02x", (unsigned char)buf[b + j]); |
| 4939 | else |
| 4940 | fprintf(out, " "); |
| 4941 | } |
| 4942 | |
| 4943 | fprintf(out, " "); |
| 4944 | for (j = 0; j < 16; j++) { |
| 4945 | if (b + j >= 0 && b + j < len) { |
| 4946 | if (isprint((unsigned char)buf[b + j])) |
| 4947 | fputc((unsigned char)buf[b + j], out); |
| 4948 | else |
| 4949 | fputc('.', out); |
| 4950 | } |
| 4951 | else |
| 4952 | fputc(' ', out); |
| 4953 | } |
| 4954 | fputc('\n', out); |
| 4955 | } |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4956 | } |
| 4957 | |
Willy Tarreau | bb86986 | 2020-04-16 10:52:41 +0200 | [diff] [blame] | 4958 | /* Tries to report the executable path name on platforms supporting this. If |
| 4959 | * not found or not possible, returns NULL. |
| 4960 | */ |
| 4961 | const char *get_exec_path() |
| 4962 | { |
| 4963 | const char *ret = NULL; |
| 4964 | |
David Carlier | 43a5685 | 2022-03-04 15:50:48 +0000 | [diff] [blame] | 4965 | #if defined(__linux__) && defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16)) |
Willy Tarreau | bb86986 | 2020-04-16 10:52:41 +0200 | [diff] [blame] | 4966 | long execfn = getauxval(AT_EXECFN); |
| 4967 | |
| 4968 | if (execfn && execfn != ENOENT) |
| 4969 | ret = (const char *)execfn; |
devnexen@gmail.com | c4e5232 | 2021-08-17 12:55:49 +0100 | [diff] [blame] | 4970 | #elif defined(__FreeBSD__) |
| 4971 | Elf_Auxinfo *auxv; |
| 4972 | for (auxv = __elf_aux_vector; auxv->a_type != AT_NULL; ++auxv) { |
| 4973 | if (auxv->a_type == AT_EXECPATH) { |
| 4974 | ret = (const char *)auxv->a_un.a_ptr; |
| 4975 | break; |
| 4976 | } |
| 4977 | } |
David Carlier | bd2cced | 2021-08-17 08:44:25 +0100 | [diff] [blame] | 4978 | #elif defined(__NetBSD__) |
| 4979 | AuxInfo *auxv; |
| 4980 | for (auxv = _dlauxinfo(); auxv->a_type != AT_NULL; ++auxv) { |
| 4981 | if (auxv->a_type == AT_SUN_EXECNAME) { |
| 4982 | ret = (const char *)auxv->a_v; |
| 4983 | break; |
| 4984 | } |
| 4985 | } |
David Carlier | 7198c70 | 2022-05-14 17:15:49 +0100 | [diff] [blame] | 4986 | #elif defined(__sun) |
| 4987 | ret = getexecname(); |
Willy Tarreau | bb86986 | 2020-04-16 10:52:41 +0200 | [diff] [blame] | 4988 | #endif |
| 4989 | return ret; |
| 4990 | } |
| 4991 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 4992 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 4993 | /* calls dladdr() or dladdr1() on <addr> and <dli>. If dladdr1 is available, |
| 4994 | * also returns the symbol size in <size>, otherwise returns 0 there. |
| 4995 | */ |
| 4996 | static int dladdr_and_size(const void *addr, Dl_info *dli, size_t *size) |
| 4997 | { |
| 4998 | int ret; |
Willy Tarreau | 7b2108c | 2021-08-30 10:15:35 +0200 | [diff] [blame] | 4999 | #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] | 5000 | const ElfW(Sym) *sym __attribute__((may_alias)); |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5001 | |
| 5002 | ret = dladdr1(addr, dli, (void **)&sym, RTLD_DL_SYMENT); |
| 5003 | if (ret) |
| 5004 | *size = sym ? sym->st_size : 0; |
| 5005 | #else |
David Carlier | ae5c42f | 2021-12-31 08:15:29 +0000 | [diff] [blame] | 5006 | #if defined(__sun) |
| 5007 | ret = dladdr((void *)addr, dli); |
| 5008 | #else |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5009 | ret = dladdr(addr, dli); |
David Carlier | ae5c42f | 2021-12-31 08:15:29 +0000 | [diff] [blame] | 5010 | #endif |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5011 | *size = 0; |
| 5012 | #endif |
| 5013 | return ret; |
| 5014 | } |
Willy Tarreau | 6419239 | 2021-05-05 09:06:21 +0200 | [diff] [blame] | 5015 | |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 5016 | /* Sets build_is_static to true if we detect a static build. Some older glibcs |
| 5017 | * tend to crash inside dlsym() in static builds, but tests show that at least |
| 5018 | * dladdr() still works (and will fail to resolve anything of course). Thus we |
| 5019 | * try to determine if we're on a static build to avoid calling dlsym() in this |
| 5020 | * case. |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 5021 | */ |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 5022 | void check_if_static_build() |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 5023 | { |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 5024 | Dl_info dli = { }; |
| 5025 | size_t size = 0; |
| 5026 | |
| 5027 | /* Now let's try to be smarter */ |
| 5028 | if (!dladdr_and_size(&main, &dli, &size)) |
| 5029 | build_is_static = 1; |
| 5030 | else |
| 5031 | build_is_static = 0; |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 5032 | } |
| 5033 | |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 5034 | INITCALL0(STG_PREPARE, check_if_static_build); |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 5035 | |
Willy Tarreau | 6419239 | 2021-05-05 09:06:21 +0200 | [diff] [blame] | 5036 | /* Tries to retrieve the address of the first occurrence symbol <name>. |
| 5037 | * Note that NULL in return is not always an error as a symbol may have that |
| 5038 | * address in special situations. |
| 5039 | */ |
| 5040 | void *get_sym_curr_addr(const char *name) |
| 5041 | { |
| 5042 | void *ptr = NULL; |
| 5043 | |
| 5044 | #ifdef RTLD_DEFAULT |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 5045 | if (!build_is_static) |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 5046 | ptr = dlsym(RTLD_DEFAULT, name); |
Willy Tarreau | 6419239 | 2021-05-05 09:06:21 +0200 | [diff] [blame] | 5047 | #endif |
| 5048 | return ptr; |
| 5049 | } |
| 5050 | |
| 5051 | |
| 5052 | /* Tries to retrieve the address of the next occurrence of symbol <name> |
| 5053 | * Note that NULL in return is not always an error as a symbol may have that |
| 5054 | * address in special situations. |
| 5055 | */ |
| 5056 | void *get_sym_next_addr(const char *name) |
| 5057 | { |
| 5058 | void *ptr = NULL; |
| 5059 | |
| 5060 | #ifdef RTLD_NEXT |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 5061 | if (!build_is_static) |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 5062 | ptr = dlsym(RTLD_NEXT, name); |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5063 | #endif |
Willy Tarreau | 6419239 | 2021-05-05 09:06:21 +0200 | [diff] [blame] | 5064 | return ptr; |
| 5065 | } |
| 5066 | |
| 5067 | #else /* elf & linux & dl */ |
| 5068 | |
| 5069 | /* no possible resolving on other platforms at the moment */ |
| 5070 | void *get_sym_curr_addr(const char *name) |
| 5071 | { |
| 5072 | return NULL; |
| 5073 | } |
| 5074 | |
| 5075 | void *get_sym_next_addr(const char *name) |
| 5076 | { |
| 5077 | return NULL; |
| 5078 | } |
| 5079 | |
| 5080 | #endif /* elf & linux & dl */ |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5081 | |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5082 | /* Tries to append to buffer <buf> some indications about the symbol at address |
| 5083 | * <addr> using the following form: |
| 5084 | * lib:+0xoffset (unresolvable address from lib's base) |
| 5085 | * main+0xoffset (unresolvable address from main (+/-)) |
| 5086 | * lib:main+0xoffset (unresolvable lib address from main (+/-)) |
| 5087 | * name (resolved exact exec address) |
| 5088 | * lib:name (resolved exact lib address) |
| 5089 | * name+0xoffset/0xsize (resolved address within exec symbol) |
| 5090 | * lib:name+0xoffset/0xsize (resolved address within lib symbol) |
| 5091 | * |
| 5092 | * The file name (lib or executable) is limited to what lies between the last |
| 5093 | * '/' and the first following '.'. An optional prefix <pfx> is prepended before |
| 5094 | * 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] | 5095 | * 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] | 5096 | * |
| 5097 | * The symbol's base address is returned, or NULL when unresolved, in order to |
| 5098 | * allow the caller to match it against known ones. |
| 5099 | */ |
Willy Tarreau | 45fd103 | 2021-01-20 14:37:59 +0100 | [diff] [blame] | 5100 | 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] | 5101 | { |
| 5102 | const struct { |
| 5103 | const void *func; |
| 5104 | const char *name; |
| 5105 | } fcts[] = { |
| 5106 | { .func = process_stream, .name = "process_stream" }, |
| 5107 | { .func = task_run_applet, .name = "task_run_applet" }, |
Willy Tarreau | 462b989 | 2022-05-18 18:06:53 +0200 | [diff] [blame] | 5108 | { .func = sc_conn_io_cb, .name = "sc_conn_io_cb" }, |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 5109 | { .func = sock_conn_iocb, .name = "sock_conn_iocb" }, |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5110 | { .func = dgram_fd_handler, .name = "dgram_fd_handler" }, |
| 5111 | { .func = listener_accept, .name = "listener_accept" }, |
Willy Tarreau | d597ec2 | 2021-01-29 14:29:06 +0100 | [diff] [blame] | 5112 | { .func = manage_global_listener_queue, .name = "manage_global_listener_queue" }, |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5113 | { .func = poller_pipe_io_handler, .name = "poller_pipe_io_handler" }, |
| 5114 | { .func = mworker_accept_wrapper, .name = "mworker_accept_wrapper" }, |
Willy Tarreau | 02922e1 | 2021-01-29 12:27:57 +0100 | [diff] [blame] | 5115 | { .func = session_expire_embryonic, .name = "session_expire_embryonic" }, |
Willy Tarreau | fb5401f | 2021-01-29 12:25:23 +0100 | [diff] [blame] | 5116 | #ifdef USE_THREAD |
| 5117 | { .func = accept_queue_process, .name = "accept_queue_process" }, |
| 5118 | #endif |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5119 | #ifdef USE_LUA |
| 5120 | { .func = hlua_process_task, .name = "hlua_process_task" }, |
| 5121 | #endif |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5122 | #ifdef SSL_MODE_ASYNC |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5123 | { .func = ssl_async_fd_free, .name = "ssl_async_fd_free" }, |
| 5124 | { .func = ssl_async_fd_handler, .name = "ssl_async_fd_handler" }, |
| 5125 | #endif |
Willy Tarreau | 8f6da64 | 2023-03-10 12:04:02 +0100 | [diff] [blame] | 5126 | #ifdef USE_QUIC |
| 5127 | { .func = quic_conn_sock_fd_iocb, .name = "quic_conn_sock_fd_iocb" }, |
| 5128 | #endif |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5129 | }; |
| 5130 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 5131 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5132 | Dl_info dli, dli_main; |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5133 | size_t size; |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5134 | const char *fname, *p; |
| 5135 | #endif |
| 5136 | int i; |
| 5137 | |
| 5138 | if (pfx) |
| 5139 | chunk_appendf(buf, "%s", pfx); |
| 5140 | |
| 5141 | for (i = 0; i < sizeof(fcts) / sizeof(fcts[0]); i++) { |
| 5142 | if (addr == fcts[i].func) { |
| 5143 | chunk_appendf(buf, "%s", fcts[i].name); |
| 5144 | return addr; |
| 5145 | } |
| 5146 | } |
| 5147 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 5148 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5149 | /* Now let's try to be smarter */ |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5150 | if (!dladdr_and_size(addr, &dli, &size)) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5151 | goto unknown; |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5152 | |
| 5153 | /* 1. prefix the library name if it's not the same object as the one |
| 5154 | * that contains the main function. The name is picked between last '/' |
| 5155 | * and first following '.'. |
| 5156 | */ |
| 5157 | if (!dladdr(main, &dli_main)) |
| 5158 | dli_main.dli_fbase = NULL; |
| 5159 | |
| 5160 | if (dli_main.dli_fbase != dli.dli_fbase) { |
| 5161 | fname = dli.dli_fname; |
| 5162 | p = strrchr(fname, '/'); |
| 5163 | if (p++) |
| 5164 | fname = p; |
| 5165 | p = strchr(fname, '.'); |
| 5166 | if (!p) |
| 5167 | p = fname + strlen(fname); |
| 5168 | |
| 5169 | chunk_appendf(buf, "%.*s:", (int)(long)(p - fname), fname); |
| 5170 | } |
| 5171 | |
| 5172 | /* 2. symbol name */ |
| 5173 | if (dli.dli_sname) { |
| 5174 | /* known, dump it and return symbol's address (exact or relative) */ |
| 5175 | chunk_appendf(buf, "%s", dli.dli_sname); |
| 5176 | if (addr != dli.dli_saddr) { |
| 5177 | chunk_appendf(buf, "+%#lx", (long)(addr - dli.dli_saddr)); |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5178 | if (size) |
| 5179 | chunk_appendf(buf, "/%#lx", (long)size); |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5180 | } |
| 5181 | return dli.dli_saddr; |
| 5182 | } |
| 5183 | else if (dli_main.dli_fbase != dli.dli_fbase) { |
| 5184 | /* unresolved symbol from a known library, report relative offset */ |
| 5185 | chunk_appendf(buf, "+%#lx", (long)(addr - dli.dli_fbase)); |
| 5186 | return NULL; |
| 5187 | } |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 5188 | #endif /* __ELF__ && !__linux__ || USE_DL */ |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5189 | unknown: |
| 5190 | /* unresolved symbol from the main file, report relative offset to main */ |
| 5191 | if ((void*)addr < (void*)main) |
| 5192 | chunk_appendf(buf, "main-%#lx", (long)((void*)main - addr)); |
| 5193 | else |
| 5194 | chunk_appendf(buf, "main+%#lx", (long)(addr - (void*)main)); |
| 5195 | return NULL; |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 5196 | } |
| 5197 | |
Willy Tarreau | 6ab7b21 | 2021-12-28 09:57:10 +0100 | [diff] [blame] | 5198 | /* On systems where this is supported, let's provide a possibility to enumerate |
| 5199 | * the list of object files. The output is appended to a buffer initialized by |
| 5200 | * the caller, with one name per line. A trailing zero is always emitted if data |
| 5201 | * are written. Only real objects are dumped (executable and .so libs). The |
| 5202 | * function returns non-zero if it dumps anything. These functions do not make |
| 5203 | * use of the trash so that it is possible for the caller to call them with the |
| 5204 | * trash on input. The output format may be platform-specific but at least one |
| 5205 | * version must emit raw object file names when argument is zero. |
| 5206 | */ |
| 5207 | #if defined(HA_HAVE_DUMP_LIBS) |
| 5208 | # if defined(HA_HAVE_DL_ITERATE_PHDR) |
| 5209 | /* the private <data> we pass below is a dump context initialized like this */ |
| 5210 | struct dl_dump_ctx { |
| 5211 | struct buffer *buf; |
| 5212 | int with_addr; |
| 5213 | }; |
| 5214 | |
| 5215 | static int dl_dump_libs_cb(struct dl_phdr_info *info, size_t size, void *data) |
| 5216 | { |
| 5217 | struct dl_dump_ctx *ctx = data; |
| 5218 | const char *fname; |
| 5219 | size_t p1, p2, beg, end; |
| 5220 | int idx; |
| 5221 | |
| 5222 | if (!info || !info->dlpi_name) |
| 5223 | goto leave; |
| 5224 | |
| 5225 | if (!*info->dlpi_name) |
| 5226 | fname = get_exec_path(); |
| 5227 | else if (strchr(info->dlpi_name, '/')) |
| 5228 | fname = info->dlpi_name; |
| 5229 | else |
| 5230 | /* else it's a VDSO or similar and we're not interested */ |
| 5231 | goto leave; |
| 5232 | |
| 5233 | if (!ctx->with_addr) |
| 5234 | goto dump_name; |
| 5235 | |
| 5236 | /* virtual addresses are relative to the load address and are per |
| 5237 | * pseudo-header, so we have to scan them all to find the furthest |
| 5238 | * one from the beginning. In this case we only dump entries if |
| 5239 | * they have at least one section. |
| 5240 | */ |
| 5241 | beg = ~0; end = 0; |
| 5242 | for (idx = 0; idx < info->dlpi_phnum; idx++) { |
| 5243 | if (!info->dlpi_phdr[idx].p_memsz) |
| 5244 | continue; |
| 5245 | p1 = info->dlpi_phdr[idx].p_vaddr; |
| 5246 | if (p1 < beg) |
| 5247 | beg = p1; |
| 5248 | p2 = p1 + info->dlpi_phdr[idx].p_memsz - 1; |
| 5249 | if (p2 > end) |
| 5250 | end = p2; |
| 5251 | } |
| 5252 | |
| 5253 | if (!idx) |
| 5254 | goto leave; |
| 5255 | |
| 5256 | chunk_appendf(ctx->buf, "0x%012llx-0x%012llx (0x%07llx) ", |
| 5257 | (ullong)info->dlpi_addr + beg, |
| 5258 | (ullong)info->dlpi_addr + end, |
| 5259 | (ullong)(end - beg + 1)); |
| 5260 | dump_name: |
| 5261 | chunk_appendf(ctx->buf, "%s\n", fname); |
| 5262 | leave: |
| 5263 | return 0; |
| 5264 | } |
| 5265 | |
| 5266 | /* dumps lib names and optionally address ranges */ |
| 5267 | int dump_libs(struct buffer *output, int with_addr) |
| 5268 | { |
| 5269 | struct dl_dump_ctx ctx = { .buf = output, .with_addr = with_addr }; |
| 5270 | size_t old_data = output->data; |
| 5271 | |
| 5272 | dl_iterate_phdr(dl_dump_libs_cb, &ctx); |
| 5273 | return output->data != old_data; |
| 5274 | } |
| 5275 | # else // no DL_ITERATE_PHDR |
| 5276 | # error "No dump_libs() function for this platform" |
| 5277 | # endif |
| 5278 | #else // no HA_HAVE_DUMP_LIBS |
| 5279 | |
| 5280 | /* unsupported platform: do not dump anything */ |
| 5281 | int dump_libs(struct buffer *output, int with_addr) |
| 5282 | { |
| 5283 | return 0; |
| 5284 | } |
| 5285 | |
| 5286 | #endif // HA_HAVE_DUMP_LIBS |
| 5287 | |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 5288 | /* |
| 5289 | * 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] | 5290 | * made of integer separated by dot characters. |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 5291 | * |
| 5292 | * First, initializes the value with <sz> as address to 0 and initializes the |
| 5293 | * array with <nums> as address to NULL. Then allocates the array with <nums> as |
| 5294 | * address updating <sz> pointed value to the size of this array. |
| 5295 | * |
| 5296 | * Returns 1 if succeeded, 0 if not. |
| 5297 | */ |
| 5298 | int parse_dotted_uints(const char *str, unsigned int **nums, size_t *sz) |
| 5299 | { |
| 5300 | unsigned int *n; |
| 5301 | const char *s, *end; |
| 5302 | |
| 5303 | s = str; |
| 5304 | *sz = 0; |
| 5305 | end = str + strlen(str); |
| 5306 | *nums = n = NULL; |
| 5307 | |
| 5308 | while (1) { |
| 5309 | unsigned int r; |
| 5310 | |
| 5311 | if (s >= end) |
| 5312 | break; |
| 5313 | |
| 5314 | r = read_uint(&s, end); |
| 5315 | /* Expected characters after having read an uint: '\0' or '.', |
| 5316 | * if '.', must not be terminal. |
| 5317 | */ |
Christopher Faulet | 4b52412 | 2021-02-11 10:42:41 +0100 | [diff] [blame] | 5318 | if (*s != '\0'&& (*s++ != '.' || s == end)) { |
| 5319 | free(n); |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 5320 | return 0; |
Christopher Faulet | 4b52412 | 2021-02-11 10:42:41 +0100 | [diff] [blame] | 5321 | } |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 5322 | |
Frédéric Lécaille | 12a7184 | 2019-02-26 18:19:48 +0100 | [diff] [blame] | 5323 | n = my_realloc2(n, (*sz + 1) * sizeof *n); |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 5324 | if (!n) |
| 5325 | return 0; |
| 5326 | |
| 5327 | n[(*sz)++] = r; |
| 5328 | } |
| 5329 | *nums = n; |
| 5330 | |
| 5331 | return 1; |
| 5332 | } |
| 5333 | |
Willy Tarreau | 4d589e7 | 2019-08-23 19:02:26 +0200 | [diff] [blame] | 5334 | |
| 5335 | /* returns the number of bytes needed to encode <v> as a varint. An inline |
| 5336 | * version exists for use with constants (__varint_bytes()). |
| 5337 | */ |
| 5338 | int varint_bytes(uint64_t v) |
| 5339 | { |
| 5340 | int len = 1; |
| 5341 | |
| 5342 | if (v >= 240) { |
| 5343 | v = (v - 240) >> 4; |
| 5344 | while (1) { |
| 5345 | len++; |
| 5346 | if (v < 128) |
| 5347 | break; |
| 5348 | v = (v - 128) >> 7; |
| 5349 | } |
| 5350 | } |
| 5351 | return len; |
| 5352 | } |
| 5353 | |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 5354 | |
| 5355 | /* Random number generator state, see below */ |
Willy Tarreau | 1544c14 | 2020-03-12 00:31:18 +0100 | [diff] [blame] | 5356 | static uint64_t ha_random_state[2] ALIGNED(2*sizeof(uint64_t)); |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 5357 | |
| 5358 | /* This is a thread-safe implementation of xoroshiro128** described below: |
| 5359 | * http://prng.di.unimi.it/ |
| 5360 | * It features a 2^128 long sequence, returns 64 high-quality bits on each call, |
| 5361 | * supports fast jumps and passes all common quality tests. It is thread-safe, |
| 5362 | * uses a double-cas on 64-bit architectures supporting it, and falls back to a |
| 5363 | * local lock on other ones. |
| 5364 | */ |
| 5365 | uint64_t ha_random64() |
| 5366 | { |
Willy Tarreau | 1544c14 | 2020-03-12 00:31:18 +0100 | [diff] [blame] | 5367 | uint64_t old[2] ALIGNED(2*sizeof(uint64_t)); |
| 5368 | uint64_t new[2] ALIGNED(2*sizeof(uint64_t)); |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 5369 | |
| 5370 | #if defined(USE_THREAD) && (!defined(HA_CAS_IS_8B) || !defined(HA_HAVE_CAS_DW)) |
| 5371 | static HA_SPINLOCK_T rand_lock; |
| 5372 | |
| 5373 | HA_SPIN_LOCK(OTHER_LOCK, &rand_lock); |
| 5374 | #endif |
| 5375 | |
| 5376 | old[0] = ha_random_state[0]; |
| 5377 | old[1] = ha_random_state[1]; |
| 5378 | |
| 5379 | #if defined(USE_THREAD) && defined(HA_CAS_IS_8B) && defined(HA_HAVE_CAS_DW) |
| 5380 | do { |
| 5381 | #endif |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 5382 | new[1] = old[0] ^ old[1]; |
| 5383 | new[0] = rotl64(old[0], 24) ^ new[1] ^ (new[1] << 16); // a, b |
| 5384 | new[1] = rotl64(new[1], 37); // c |
| 5385 | |
| 5386 | #if defined(USE_THREAD) && defined(HA_CAS_IS_8B) && defined(HA_HAVE_CAS_DW) |
| 5387 | } while (unlikely(!_HA_ATOMIC_DWCAS(ha_random_state, old, new))); |
| 5388 | #else |
| 5389 | ha_random_state[0] = new[0]; |
| 5390 | ha_random_state[1] = new[1]; |
| 5391 | #if defined(USE_THREAD) |
| 5392 | HA_SPIN_UNLOCK(OTHER_LOCK, &rand_lock); |
| 5393 | #endif |
| 5394 | #endif |
Willy Tarreau | b2475a1 | 2021-05-09 10:26:14 +0200 | [diff] [blame] | 5395 | return rotl64(old[0] * 5, 7) * 9; |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 5396 | } |
| 5397 | |
| 5398 | /* seeds the random state using up to <len> bytes from <seed>, starting with |
| 5399 | * the first non-zero byte. |
| 5400 | */ |
| 5401 | void ha_random_seed(const unsigned char *seed, size_t len) |
| 5402 | { |
| 5403 | size_t pos; |
| 5404 | |
| 5405 | /* the seed must not be all zeroes, so we pre-fill it with alternating |
| 5406 | * bits and overwrite part of them with the block starting at the first |
| 5407 | * non-zero byte from the seed. |
| 5408 | */ |
| 5409 | memset(ha_random_state, 0x55, sizeof(ha_random_state)); |
| 5410 | |
| 5411 | for (pos = 0; pos < len; pos++) |
| 5412 | if (seed[pos] != 0) |
| 5413 | break; |
| 5414 | |
| 5415 | if (pos == len) |
| 5416 | return; |
| 5417 | |
| 5418 | seed += pos; |
| 5419 | len -= pos; |
| 5420 | |
| 5421 | if (len > sizeof(ha_random_state)) |
| 5422 | len = sizeof(ha_random_state); |
| 5423 | |
| 5424 | memcpy(ha_random_state, seed, len); |
| 5425 | } |
| 5426 | |
| 5427 | /* This causes a jump to (dist * 2^96) places in the pseudo-random sequence, |
| 5428 | * and is equivalent to calling ha_random64() as many times. It is used to |
| 5429 | * provide non-overlapping sequences of 2^96 numbers (~7*10^28) to up to 2^32 |
| 5430 | * different generators (i.e. different processes after a fork). The <dist> |
| 5431 | * argument is the distance to jump to and is used in a loop so it rather not |
| 5432 | * be too large if the processing time is a concern. |
| 5433 | * |
| 5434 | * BEWARE: this function is NOT thread-safe and must not be called during |
| 5435 | * concurrent accesses to ha_random64(). |
| 5436 | */ |
| 5437 | void ha_random_jump96(uint32_t dist) |
| 5438 | { |
| 5439 | while (dist--) { |
| 5440 | uint64_t s0 = 0; |
| 5441 | uint64_t s1 = 0; |
| 5442 | int b; |
| 5443 | |
| 5444 | for (b = 0; b < 64; b++) { |
| 5445 | if ((0xd2a98b26625eee7bULL >> b) & 1) { |
| 5446 | s0 ^= ha_random_state[0]; |
| 5447 | s1 ^= ha_random_state[1]; |
| 5448 | } |
| 5449 | ha_random64(); |
| 5450 | } |
| 5451 | |
| 5452 | for (b = 0; b < 64; b++) { |
| 5453 | if ((0xdddf9b1090aa7ac1ULL >> b) & 1) { |
| 5454 | s0 ^= ha_random_state[0]; |
| 5455 | s1 ^= ha_random_state[1]; |
| 5456 | } |
| 5457 | ha_random64(); |
| 5458 | } |
| 5459 | ha_random_state[0] = s0; |
| 5460 | ha_random_state[1] = s1; |
| 5461 | } |
| 5462 | } |
| 5463 | |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 5464 | /* Generates an RFC4122 UUID into chunk <output> which must be at least 37 |
| 5465 | * bytes large. |
| 5466 | */ |
| 5467 | void ha_generate_uuid(struct buffer *output) |
| 5468 | { |
| 5469 | uint32_t rnd[4]; |
| 5470 | uint64_t last; |
| 5471 | |
| 5472 | last = ha_random64(); |
| 5473 | rnd[0] = last; |
| 5474 | rnd[1] = last >> 32; |
| 5475 | |
| 5476 | last = ha_random64(); |
| 5477 | rnd[2] = last; |
| 5478 | rnd[3] = last >> 32; |
| 5479 | |
| 5480 | chunk_printf(output, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx", |
| 5481 | rnd[0], |
| 5482 | rnd[1] & 0xFFFF, |
| 5483 | ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version |
| 5484 | (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10), |
| 5485 | (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull); |
| 5486 | } |
| 5487 | |
| 5488 | |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5489 | /* only used by parse_line() below. It supports writing in place provided that |
| 5490 | * <in> is updated to the next location before calling it. In that case, the |
| 5491 | * char at <in> may be overwritten. |
| 5492 | */ |
| 5493 | #define EMIT_CHAR(x) \ |
| 5494 | do { \ |
| 5495 | char __c = (char)(x); \ |
| 5496 | if ((opts & PARSE_OPT_INPLACE) && out+outpos > in) \ |
| 5497 | err |= PARSE_ERR_OVERLAP; \ |
| 5498 | if (outpos >= outmax) \ |
| 5499 | err |= PARSE_ERR_TOOLARGE; \ |
| 5500 | if (!err) \ |
| 5501 | out[outpos] = __c; \ |
| 5502 | outpos++; \ |
| 5503 | } while (0) |
| 5504 | |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 5505 | /* Parse <in>, copy it into <out> split into isolated words whose pointers |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5506 | * are put in <args>. If more than <outlen> bytes have to be emitted, the |
| 5507 | * extraneous ones are not emitted but <outlen> is updated so that the caller |
| 5508 | * knows how much to realloc. Similarly, <args> are not updated beyond <nbargs> |
| 5509 | * but the returned <nbargs> indicates how many were found. All trailing args |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5510 | * up to <nbargs> point to the trailing zero, and as long as <nbargs> is > 0, |
| 5511 | * it is guaranteed that at least one arg will point to the zero. It is safe |
| 5512 | * to call it with a NULL <args> if <nbargs> is 0. |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5513 | * |
| 5514 | * <out> may overlap with <in> provided that it never goes further, in which |
| 5515 | * case the parser will accept to perform in-place parsing and unquoting/ |
| 5516 | * unescaping but only if environment variables do not lead to expansion that |
| 5517 | * causes overlapping, otherwise the input string being destroyed, the error |
| 5518 | * will not be recoverable. Note that even during out-of-place <in> will |
| 5519 | * experience temporary modifications in-place for variable resolution and must |
| 5520 | * be writable, and will also receive zeroes to delimit words when using |
| 5521 | * in-place copy. Parsing options <opts> taken from PARSE_OPT_*. Return value |
| 5522 | * is zero on success otherwise a bitwise-or of PARSE_ERR_*. Upon error, the |
| 5523 | * starting point of the first invalid character sequence or unmatched |
| 5524 | * quote/brace is reported in <errptr> if not NULL. When using in-place parsing |
| 5525 | * error reporting might be difficult since zeroes will have been inserted into |
| 5526 | * the string. One solution for the caller may consist in replacing all args |
| 5527 | * delimiters with spaces in this case. |
| 5528 | */ |
Maximilian Mader | 29c6cd7 | 2021-06-06 00:50:21 +0200 | [diff] [blame] | 5529 | 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] | 5530 | { |
| 5531 | char *quote = NULL; |
| 5532 | char *brace = NULL; |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5533 | char *word_expand = NULL; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5534 | unsigned char hex1, hex2; |
| 5535 | size_t outmax = *outlen; |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5536 | int argsmax = *nbargs - 1; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5537 | size_t outpos = 0; |
| 5538 | int squote = 0; |
| 5539 | int dquote = 0; |
| 5540 | int arg = 0; |
| 5541 | uint32_t err = 0; |
| 5542 | |
| 5543 | *nbargs = 0; |
| 5544 | *outlen = 0; |
| 5545 | |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5546 | /* argsmax may be -1 here, protecting args[] from any write */ |
| 5547 | if (arg < argsmax) |
| 5548 | args[arg] = out; |
| 5549 | |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5550 | while (1) { |
| 5551 | if (*in >= '-' && *in != '\\') { |
| 5552 | /* speedup: directly send all regular chars starting |
| 5553 | * with '-', '.', '/', alnum etc... |
| 5554 | */ |
| 5555 | EMIT_CHAR(*in++); |
| 5556 | continue; |
| 5557 | } |
| 5558 | else if (*in == '\0' || *in == '\n' || *in == '\r') { |
| 5559 | /* end of line */ |
| 5560 | break; |
| 5561 | } |
| 5562 | else if (*in == '#' && (opts & PARSE_OPT_SHARP) && !squote && !dquote) { |
| 5563 | /* comment */ |
| 5564 | break; |
| 5565 | } |
| 5566 | else if (*in == '"' && !squote && (opts & PARSE_OPT_DQUOTE)) { /* double quote outside single quotes */ |
| 5567 | if (dquote) { |
| 5568 | dquote = 0; |
| 5569 | quote = NULL; |
| 5570 | } |
| 5571 | else { |
| 5572 | dquote = 1; |
| 5573 | quote = in; |
| 5574 | } |
| 5575 | in++; |
| 5576 | continue; |
| 5577 | } |
| 5578 | else if (*in == '\'' && !dquote && (opts & PARSE_OPT_SQUOTE)) { /* single quote outside double quotes */ |
| 5579 | if (squote) { |
| 5580 | squote = 0; |
| 5581 | quote = NULL; |
| 5582 | } |
| 5583 | else { |
| 5584 | squote = 1; |
| 5585 | quote = in; |
| 5586 | } |
| 5587 | in++; |
| 5588 | continue; |
| 5589 | } |
| 5590 | else if (*in == '\\' && !squote && (opts & PARSE_OPT_BKSLASH)) { |
| 5591 | /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their |
| 5592 | * C equivalent value but only when they have a special meaning and within |
| 5593 | * double quotes for some of them. Other combinations left unchanged (eg: \1). |
| 5594 | */ |
| 5595 | char tosend = *in; |
| 5596 | |
| 5597 | switch (in[1]) { |
| 5598 | case ' ': |
| 5599 | case '\\': |
| 5600 | tosend = in[1]; |
| 5601 | in++; |
| 5602 | break; |
| 5603 | |
| 5604 | case 't': |
| 5605 | tosend = '\t'; |
| 5606 | in++; |
| 5607 | break; |
| 5608 | |
| 5609 | case 'n': |
| 5610 | tosend = '\n'; |
| 5611 | in++; |
| 5612 | break; |
| 5613 | |
| 5614 | case 'r': |
| 5615 | tosend = '\r'; |
| 5616 | in++; |
| 5617 | break; |
| 5618 | |
| 5619 | case '#': |
| 5620 | /* escaping of "#" only if comments are supported */ |
| 5621 | if (opts & PARSE_OPT_SHARP) |
| 5622 | in++; |
| 5623 | tosend = *in; |
| 5624 | break; |
| 5625 | |
| 5626 | case '\'': |
| 5627 | /* escaping of "'" only outside single quotes and only if single quotes are supported */ |
| 5628 | if (opts & PARSE_OPT_SQUOTE && !squote) |
| 5629 | in++; |
| 5630 | tosend = *in; |
| 5631 | break; |
| 5632 | |
| 5633 | case '"': |
| 5634 | /* escaping of '"' only outside single quotes and only if double quotes are supported */ |
| 5635 | if (opts & PARSE_OPT_DQUOTE && !squote) |
| 5636 | in++; |
| 5637 | tosend = *in; |
| 5638 | break; |
| 5639 | |
| 5640 | case '$': |
| 5641 | /* escaping of '$' only inside double quotes and only if env supported */ |
| 5642 | if (opts & PARSE_OPT_ENV && dquote) |
| 5643 | in++; |
| 5644 | tosend = *in; |
| 5645 | break; |
| 5646 | |
| 5647 | case 'x': |
| 5648 | if (!ishex(in[2]) || !ishex(in[3])) { |
| 5649 | /* invalid or incomplete hex sequence */ |
| 5650 | err |= PARSE_ERR_HEX; |
| 5651 | if (errptr) |
| 5652 | *errptr = in; |
| 5653 | goto leave; |
| 5654 | } |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 5655 | hex1 = toupper((unsigned char)in[2]) - '0'; |
| 5656 | hex2 = toupper((unsigned char)in[3]) - '0'; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5657 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 5658 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 5659 | tosend = (hex1 << 4) + hex2; |
| 5660 | in += 3; |
| 5661 | break; |
| 5662 | |
| 5663 | default: |
| 5664 | /* other combinations are not escape sequences */ |
| 5665 | break; |
| 5666 | } |
| 5667 | |
| 5668 | in++; |
| 5669 | EMIT_CHAR(tosend); |
| 5670 | } |
| 5671 | else if (isspace((unsigned char)*in) && !squote && !dquote) { |
| 5672 | /* a non-escaped space is an argument separator */ |
| 5673 | while (isspace((unsigned char)*in)) |
| 5674 | in++; |
| 5675 | EMIT_CHAR(0); |
| 5676 | arg++; |
| 5677 | if (arg < argsmax) |
| 5678 | args[arg] = out + outpos; |
| 5679 | else |
| 5680 | err |= PARSE_ERR_TOOMANY; |
| 5681 | } |
| 5682 | else if (*in == '$' && (opts & PARSE_OPT_ENV) && (dquote || !(opts & PARSE_OPT_DQUOTE))) { |
| 5683 | /* environment variables are evaluated anywhere, or only |
| 5684 | * inside double quotes if they are supported. |
| 5685 | */ |
| 5686 | char *var_name; |
| 5687 | char save_char; |
Willy Tarreau | a46f1af | 2021-05-06 10:25:11 +0200 | [diff] [blame] | 5688 | const char *value; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5689 | |
| 5690 | in++; |
| 5691 | |
| 5692 | if (*in == '{') |
| 5693 | brace = in++; |
| 5694 | |
Willy Tarreau | a46f1af | 2021-05-06 10:25:11 +0200 | [diff] [blame] | 5695 | if (!isalpha((unsigned char)*in) && *in != '_' && *in != '.') { |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5696 | /* unacceptable character in variable name */ |
| 5697 | err |= PARSE_ERR_VARNAME; |
| 5698 | if (errptr) |
| 5699 | *errptr = in; |
| 5700 | goto leave; |
| 5701 | } |
| 5702 | |
| 5703 | var_name = in; |
Willy Tarreau | a46f1af | 2021-05-06 10:25:11 +0200 | [diff] [blame] | 5704 | if (*in == '.') |
| 5705 | in++; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5706 | while (isalnum((unsigned char)*in) || *in == '_') |
| 5707 | in++; |
| 5708 | |
| 5709 | save_char = *in; |
| 5710 | *in = '\0'; |
Willy Tarreau | a46f1af | 2021-05-06 10:25:11 +0200 | [diff] [blame] | 5711 | if (unlikely(*var_name == '.')) { |
| 5712 | /* internal pseudo-variables */ |
| 5713 | if (strcmp(var_name, ".LINE") == 0) |
| 5714 | value = ultoa(global.cfg_curr_line); |
| 5715 | else if (strcmp(var_name, ".FILE") == 0) |
| 5716 | value = global.cfg_curr_file; |
| 5717 | else if (strcmp(var_name, ".SECTION") == 0) |
| 5718 | value = global.cfg_curr_section; |
| 5719 | else { |
| 5720 | /* unsupported internal variable name */ |
| 5721 | err |= PARSE_ERR_VARNAME; |
| 5722 | if (errptr) |
| 5723 | *errptr = var_name; |
| 5724 | goto leave; |
| 5725 | } |
| 5726 | } else { |
| 5727 | value = getenv(var_name); |
| 5728 | } |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5729 | *in = save_char; |
| 5730 | |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5731 | /* support for '[*]' sequence to force word expansion, |
| 5732 | * only available inside braces */ |
| 5733 | if (*in == '[' && brace && (opts & PARSE_OPT_WORD_EXPAND)) { |
| 5734 | word_expand = in++; |
| 5735 | |
| 5736 | if (*in++ != '*' || *in++ != ']') { |
| 5737 | err |= PARSE_ERR_WRONG_EXPAND; |
| 5738 | if (errptr) |
| 5739 | *errptr = word_expand; |
| 5740 | goto leave; |
| 5741 | } |
| 5742 | } |
| 5743 | |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5744 | if (brace) { |
Willy Tarreau | ec347b1 | 2021-11-18 17:42:50 +0100 | [diff] [blame] | 5745 | if (*in == '-') { |
| 5746 | /* default value starts just after the '-' */ |
| 5747 | if (!value) |
| 5748 | value = in + 1; |
| 5749 | |
| 5750 | while (*in && *in != '}') |
| 5751 | in++; |
| 5752 | if (!*in) |
| 5753 | goto no_brace; |
| 5754 | *in = 0; // terminate the default value |
| 5755 | } |
| 5756 | else if (*in != '}') { |
| 5757 | no_brace: |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5758 | /* unmatched brace */ |
| 5759 | err |= PARSE_ERR_BRACE; |
| 5760 | if (errptr) |
| 5761 | *errptr = brace; |
| 5762 | goto leave; |
| 5763 | } |
Willy Tarreau | ec347b1 | 2021-11-18 17:42:50 +0100 | [diff] [blame] | 5764 | |
| 5765 | /* brace found, skip it */ |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5766 | in++; |
| 5767 | brace = NULL; |
| 5768 | } |
| 5769 | |
| 5770 | if (value) { |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5771 | while (*value) { |
| 5772 | /* expand as individual parameters on a space character */ |
Willy Tarreau | fe2cc41 | 2020-10-01 18:04:40 +0200 | [diff] [blame] | 5773 | if (word_expand && isspace((unsigned char)*value)) { |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5774 | EMIT_CHAR(0); |
| 5775 | ++arg; |
| 5776 | if (arg < argsmax) |
| 5777 | args[arg] = out + outpos; |
| 5778 | else |
| 5779 | err |= PARSE_ERR_TOOMANY; |
| 5780 | |
| 5781 | /* skip consecutive spaces */ |
Willy Tarreau | fe2cc41 | 2020-10-01 18:04:40 +0200 | [diff] [blame] | 5782 | while (isspace((unsigned char)*++value)) |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5783 | ; |
| 5784 | } else { |
| 5785 | EMIT_CHAR(*value++); |
| 5786 | } |
| 5787 | } |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5788 | } |
Frédéric Lécaille | 51fad64 | 2023-11-30 09:26:05 +0100 | [diff] [blame] | 5789 | else { |
| 5790 | /* An unmatched environment variable was parsed. |
| 5791 | * Let's skip the trailing double-quote character |
| 5792 | * and spaces. |
| 5793 | */ |
| 5794 | if (likely(*var_name != '.') && *in == '"') { |
| 5795 | in++; |
| 5796 | while (isspace((unsigned char)*in)) |
| 5797 | in++; |
| 5798 | if (dquote) { |
| 5799 | dquote = 0; |
| 5800 | quote = NULL; |
| 5801 | } |
| 5802 | } |
| 5803 | } |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5804 | word_expand = NULL; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5805 | } |
| 5806 | else { |
| 5807 | /* any other regular char */ |
| 5808 | EMIT_CHAR(*in++); |
| 5809 | } |
| 5810 | } |
| 5811 | |
| 5812 | /* end of output string */ |
| 5813 | EMIT_CHAR(0); |
Erwan Le Goas | 8a6767d | 2022-09-23 15:06:34 +0200 | [diff] [blame] | 5814 | |
Willy Tarreau | 94ab139 | 2022-10-03 08:27:55 +0200 | [diff] [blame] | 5815 | /* Don't add an empty arg after trailing spaces. Note that args[arg] |
| 5816 | * may contain some distances relative to NULL if <out> was NULL, or |
| 5817 | * pointers beyond the end of <out> in case <outlen> is too short, thus |
| 5818 | * we must not dereference it. |
Erwan Le Goas | 8a6767d | 2022-09-23 15:06:34 +0200 | [diff] [blame] | 5819 | */ |
Willy Tarreau | 94ab139 | 2022-10-03 08:27:55 +0200 | [diff] [blame] | 5820 | if (arg < argsmax && args[arg] != out + outpos - 1) |
Erwan Le Goas | 8a6767d | 2022-09-23 15:06:34 +0200 | [diff] [blame] | 5821 | arg++; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5822 | |
| 5823 | if (quote) { |
| 5824 | /* unmatched quote */ |
| 5825 | err |= PARSE_ERR_QUOTE; |
| 5826 | if (errptr) |
| 5827 | *errptr = quote; |
| 5828 | goto leave; |
| 5829 | } |
| 5830 | leave: |
| 5831 | *nbargs = arg; |
| 5832 | *outlen = outpos; |
| 5833 | |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5834 | /* empty all trailing args by making them point to the trailing zero, |
| 5835 | * at least the last one in any case. |
| 5836 | */ |
| 5837 | if (arg > argsmax) |
| 5838 | arg = argsmax; |
| 5839 | |
| 5840 | while (arg >= 0 && arg <= argsmax) |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5841 | args[arg++] = out + outpos - 1; |
| 5842 | |
| 5843 | return err; |
| 5844 | } |
| 5845 | #undef EMIT_CHAR |
| 5846 | |
Willy Tarreau | c54e5ad | 2020-06-25 09:15:40 +0200 | [diff] [blame] | 5847 | /* This is used to sanitize an input line that's about to be used for error reporting. |
| 5848 | * It will adjust <line> to print approximately <width> chars around <pos>, trying to |
| 5849 | * preserve the beginning, with leading or trailing "..." when the line is truncated. |
| 5850 | * If non-printable chars are present in the output. It returns the new offset <pos> |
| 5851 | * in the modified line. Non-printable characters are replaced with '?'. <width> must |
| 5852 | * be at least 6 to support two "..." otherwise the result is undefined. The line |
| 5853 | * itself must have at least 7 chars allocated for the same reason. |
| 5854 | */ |
| 5855 | size_t sanitize_for_printing(char *line, size_t pos, size_t width) |
| 5856 | { |
| 5857 | size_t shift = 0; |
| 5858 | char *out = line; |
| 5859 | char *in = line; |
| 5860 | char *end = line + width; |
| 5861 | |
| 5862 | if (pos >= width) { |
| 5863 | /* if we have to shift, we'll be out of context, so let's |
| 5864 | * try to put <pos> at the center of width. |
| 5865 | */ |
| 5866 | shift = pos - width / 2; |
| 5867 | in += shift + 3; |
| 5868 | end = out + width - 3; |
| 5869 | out[0] = out[1] = out[2] = '.'; |
| 5870 | out += 3; |
| 5871 | } |
| 5872 | |
| 5873 | while (out < end && *in) { |
| 5874 | if (isspace((unsigned char)*in)) |
| 5875 | *out++ = ' '; |
| 5876 | else if (isprint((unsigned char)*in)) |
| 5877 | *out++ = *in; |
| 5878 | else |
| 5879 | *out++ = '?'; |
| 5880 | in++; |
| 5881 | } |
| 5882 | |
| 5883 | if (end < line + width) { |
| 5884 | out[0] = out[1] = out[2] = '.'; |
| 5885 | out += 3; |
| 5886 | } |
| 5887 | |
| 5888 | *out++ = 0; |
| 5889 | return pos - shift; |
| 5890 | } |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 5891 | |
Willy Tarreau | e33c4b3 | 2021-03-12 18:59:31 +0100 | [diff] [blame] | 5892 | /* Update array <fp> with the fingerprint of word <word> by counting the |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5893 | * transitions between characters. <fp> is a 1024-entries array indexed as |
| 5894 | * 32*from+to. Positions for 'from' and 'to' are: |
Willy Tarreau | 9294e88 | 2021-03-15 09:34:27 +0100 | [diff] [blame] | 5895 | * 1..26=letter, 27=digit, 28=other/begin/end. |
| 5896 | * 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] | 5897 | */ |
Willy Tarreau | e33c4b3 | 2021-03-12 18:59:31 +0100 | [diff] [blame] | 5898 | void update_word_fingerprint(uint8_t *fp, const char *word) |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5899 | { |
| 5900 | const char *p; |
| 5901 | int from, to; |
| 5902 | int c; |
| 5903 | |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5904 | from = 28; // begin |
| 5905 | for (p = word; *p; p++) { |
| 5906 | c = tolower(*p); |
| 5907 | switch(c) { |
Willy Tarreau | 9294e88 | 2021-03-15 09:34:27 +0100 | [diff] [blame] | 5908 | case 'a'...'z': to = c - 'a' + 1; break; |
| 5909 | case 'A'...'Z': to = tolower(c) - 'a' + 1; break; |
| 5910 | case '0'...'9': to = 27; break; |
| 5911 | default: to = 28; break; |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5912 | } |
Willy Tarreau | 9294e88 | 2021-03-15 09:34:27 +0100 | [diff] [blame] | 5913 | fp[to] = 1; |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5914 | fp[32 * from + to]++; |
| 5915 | from = to; |
| 5916 | } |
| 5917 | to = 28; // end |
| 5918 | fp[32 * from + to]++; |
| 5919 | } |
| 5920 | |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5921 | /* This function hashes a word, scramble is the anonymizing key, returns |
| 5922 | * the hashed word when the key (scramble) != 0, else returns the word. |
| 5923 | * This function can be called NB_L_HASH_WORD times in a row, don't call |
| 5924 | * it if you called it more than NB_L_HASH_WORD. |
| 5925 | */ |
| 5926 | const char *hash_anon(uint32_t scramble, const char *string2hash, const char *prefix, const char *suffix) |
| 5927 | { |
| 5928 | index_hash++; |
Erwan Le Goas | d2605cf | 2022-09-21 16:24:23 +0200 | [diff] [blame] | 5929 | if (index_hash == NB_L_HASH_WORD) |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5930 | index_hash = 0; |
| 5931 | |
| 5932 | /* don't hash empty strings */ |
| 5933 | if (!string2hash[0] || (string2hash[0] == ' ' && string2hash[1] == 0)) |
| 5934 | return string2hash; |
| 5935 | |
| 5936 | if (scramble != 0) { |
| 5937 | snprintf(hash_word[index_hash], sizeof(hash_word[index_hash]), "%s%06x%s", |
| 5938 | prefix, HA_ANON(scramble, string2hash, strlen(string2hash)), suffix); |
| 5939 | return hash_word[index_hash]; |
| 5940 | } |
| 5941 | else |
| 5942 | return string2hash; |
| 5943 | } |
| 5944 | |
| 5945 | /* This function hashes or not an ip address ipstring, scramble is the anonymizing |
| 5946 | * key, returns the hashed ip with his port or ipstring when there is nothing to hash. |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5947 | * Put hasport equal 0 to point out ipstring has no port, else put an other int. |
| 5948 | * Without port, return a simple hash or ipstring. |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5949 | */ |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5950 | const char *hash_ipanon(uint32_t scramble, char *ipstring, int hasport) |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5951 | { |
| 5952 | char *errmsg = NULL; |
| 5953 | struct sockaddr_storage *sa; |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5954 | struct sockaddr_storage ss; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5955 | char addr[46]; |
| 5956 | int port; |
| 5957 | |
| 5958 | index_hash++; |
Erwan Le Goas | d2605cf | 2022-09-21 16:24:23 +0200 | [diff] [blame] | 5959 | if (index_hash == NB_L_HASH_WORD) { |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5960 | index_hash = 0; |
| 5961 | } |
| 5962 | |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5963 | if (scramble == 0) { |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5964 | return ipstring; |
| 5965 | } |
Christopher Faulet | 015bbc2 | 2022-09-29 11:53:07 +0200 | [diff] [blame] | 5966 | if (strcmp(ipstring, "localhost") == 0 || |
| 5967 | strcmp(ipstring, "stdout") == 0 || |
| 5968 | strcmp(ipstring, "stderr") == 0 || |
| 5969 | strncmp(ipstring, "fd@", 3) == 0 || |
| 5970 | strncmp(ipstring, "sockpair@", 9) == 0) { |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5971 | return ipstring; |
| 5972 | } |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5973 | else { |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5974 | if (hasport == 0) { |
| 5975 | memset(&ss, 0, sizeof(ss)); |
| 5976 | if (str2ip2(ipstring, &ss, 1) == NULL) { |
| 5977 | return HA_ANON_STR(scramble, ipstring); |
| 5978 | } |
| 5979 | sa = &ss; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5980 | } |
| 5981 | else { |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5982 | sa = str2sa_range(ipstring, NULL, NULL, NULL, NULL, NULL, &errmsg, NULL, NULL, |
Christopher Faulet | 7e50e4b | 2022-09-29 11:46:34 +0200 | [diff] [blame] | 5983 | PA_O_PORT_OK | PA_O_STREAM | PA_O_DGRAM | PA_O_XPRT | PA_O_CONNECT | |
| 5984 | PA_O_PORT_RANGE | PA_O_PORT_OFS | PA_O_RESOLVE); |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5985 | if (sa == NULL) { |
| 5986 | return HA_ANON_STR(scramble, ipstring); |
| 5987 | } |
| 5988 | } |
| 5989 | addr_to_str(sa, addr, sizeof(addr)); |
| 5990 | port = get_host_port(sa); |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5991 | |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5992 | switch(sa->ss_family) { |
| 5993 | case AF_INET: |
| 5994 | if (strncmp(addr, "127", 3) == 0 || strncmp(addr, "255", 3) == 0 || strncmp(addr, "0", 1) == 0) { |
| 5995 | return ipstring; |
| 5996 | } |
| 5997 | else { |
| 5998 | if (port != 0) { |
| 5999 | snprintf(hash_word[index_hash], sizeof(hash_word[index_hash]), "IPV4(%06x):%d", HA_ANON(scramble, addr, strlen(addr)), port); |
| 6000 | return hash_word[index_hash]; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 6001 | } |
| 6002 | else { |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 6003 | snprintf(hash_word[index_hash], sizeof(hash_word[index_hash]), "IPV4(%06x)", HA_ANON(scramble, addr, strlen(addr))); |
| 6004 | return hash_word[index_hash]; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 6005 | } |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 6006 | } |
| 6007 | break; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 6008 | |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 6009 | case AF_INET6: |
| 6010 | if (strcmp(addr, "::1") == 0) { |
| 6011 | return ipstring; |
| 6012 | } |
| 6013 | else { |
| 6014 | if (port != 0) { |
| 6015 | snprintf(hash_word[index_hash], sizeof(hash_word[index_hash]), "IPV6(%06x):%d", HA_ANON(scramble, addr, strlen(addr)), port); |
| 6016 | return hash_word[index_hash]; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 6017 | } |
| 6018 | else { |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 6019 | snprintf(hash_word[index_hash], sizeof(hash_word[index_hash]), "IPV6(%06x)", HA_ANON(scramble, addr, strlen(addr))); |
| 6020 | return hash_word[index_hash]; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 6021 | } |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 6022 | } |
| 6023 | break; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 6024 | |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 6025 | case AF_UNIX: |
| 6026 | return HA_ANON_STR(scramble, ipstring); |
| 6027 | break; |
| 6028 | |
| 6029 | default: |
| 6030 | return ipstring; |
| 6031 | break; |
| 6032 | }; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 6033 | } |
| 6034 | return ipstring; |
| 6035 | } |
| 6036 | |
Willy Tarreau | e33c4b3 | 2021-03-12 18:59:31 +0100 | [diff] [blame] | 6037 | /* Initialize array <fp> with the fingerprint of word <word> by counting the |
| 6038 | * transitions between characters. <fp> is a 1024-entries array indexed as |
| 6039 | * 32*from+to. Positions for 'from' and 'to' are: |
| 6040 | * 0..25=letter, 26=digit, 27=other, 28=begin, 29=end, others unused. |
| 6041 | */ |
| 6042 | void make_word_fingerprint(uint8_t *fp, const char *word) |
| 6043 | { |
| 6044 | memset(fp, 0, 1024); |
| 6045 | update_word_fingerprint(fp, word); |
| 6046 | } |
| 6047 | |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 6048 | /* Return the distance between two word fingerprints created by function |
| 6049 | * 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] | 6050 | * the differences between each location. |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 6051 | */ |
| 6052 | int word_fingerprint_distance(const uint8_t *fp1, const uint8_t *fp2) |
| 6053 | { |
| 6054 | int i, k, dist = 0; |
| 6055 | |
| 6056 | for (i = 0; i < 1024; i++) { |
| 6057 | k = (int)fp1[i] - (int)fp2[i]; |
Willy Tarreau | 714c4c1 | 2021-03-15 09:44:53 +0100 | [diff] [blame] | 6058 | dist += abs(k); |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 6059 | } |
| 6060 | return dist; |
| 6061 | } |
| 6062 | |
William Lallemand | 3aeb3f9 | 2021-08-21 23:59:56 +0200 | [diff] [blame] | 6063 | /* |
| 6064 | * This function compares the loaded openssl version with a string <version> |
| 6065 | * This function use the same return code as compare_current_version: |
| 6066 | * |
| 6067 | * -1 : the version in argument is older than the current openssl version |
| 6068 | * 0 : the version in argument is the same as the current openssl version |
| 6069 | * 1 : the version in argument is newer than the current openssl version |
| 6070 | * |
| 6071 | * Or some errors: |
| 6072 | * -2 : openssl is not available on this process |
| 6073 | * -3 : the version in argument is not parsable |
| 6074 | */ |
| 6075 | int openssl_compare_current_version(const char *version) |
| 6076 | { |
| 6077 | #ifdef USE_OPENSSL |
| 6078 | int numversion; |
| 6079 | |
| 6080 | numversion = openssl_version_parser(version); |
| 6081 | if (numversion == 0) |
| 6082 | return -3; |
| 6083 | |
| 6084 | if (numversion < OPENSSL_VERSION_NUMBER) |
| 6085 | return -1; |
| 6086 | else if (numversion > OPENSSL_VERSION_NUMBER) |
| 6087 | return 1; |
| 6088 | else |
| 6089 | return 0; |
| 6090 | #else |
| 6091 | return -2; |
| 6092 | #endif |
| 6093 | } |
| 6094 | |
Remi Tricot-Le Breton | b01179a | 2021-10-11 15:34:12 +0200 | [diff] [blame] | 6095 | /* |
| 6096 | * This function compares the loaded openssl name with a string <name> |
| 6097 | * This function returns 0 if the OpenSSL name starts like the passed parameter, |
| 6098 | * 1 otherwise. |
| 6099 | */ |
| 6100 | int openssl_compare_current_name(const char *name) |
| 6101 | { |
| 6102 | #ifdef USE_OPENSSL |
| 6103 | int name_len = 0; |
| 6104 | const char *openssl_version = OpenSSL_version(OPENSSL_VERSION); |
| 6105 | |
| 6106 | if (name) { |
| 6107 | name_len = strlen(name); |
| 6108 | if (strlen(name) <= strlen(openssl_version)) |
| 6109 | return strncmp(openssl_version, name, name_len); |
| 6110 | } |
| 6111 | #endif |
| 6112 | return 1; |
| 6113 | } |
| 6114 | |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6115 | #if defined(RTLD_DEFAULT) || defined(RTLD_NEXT) |
| 6116 | /* redefine dlopen() so that we can detect unexpected replacement of some |
| 6117 | * critical symbols, typically init/alloc/free functions coming from alternate |
| 6118 | * libraries. When called, a tainted flag is set (TAINTED_SHARED_LIBS). |
Willy Tarreau | c3b297d | 2023-03-22 17:01:32 +0100 | [diff] [blame] | 6119 | * It's important to understand that the dynamic linker will present the |
| 6120 | * first loaded of each symbol to all libs, so that if haproxy is linked |
| 6121 | * with a new lib that uses a static inline or a #define to replace an old |
| 6122 | * function, and a dependency was linked against an older version of that |
| 6123 | * lib that had a function there, that lib would use all of the newer |
| 6124 | * versions of the functions that are already loaded in haproxy, except |
| 6125 | * for that unique function which would continue to be the old one. This |
| 6126 | * creates all sort of problems when init code allocates smaller structs |
| 6127 | * than required for example but uses new functions on them, etc. Thus what |
| 6128 | * we do here is to try to detect API consistency: we take a fingerprint of |
| 6129 | * a number of known functions, and verify that if they change in a loaded |
| 6130 | * library, either there all appeared or all disappeared, but not partially. |
| 6131 | * We can check up to 64 symbols that belong to individual groups that are |
| 6132 | * checked together. |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6133 | */ |
| 6134 | void *dlopen(const char *filename, int flags) |
| 6135 | { |
| 6136 | static void *(*_dlopen)(const char *filename, int flags); |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6137 | struct { |
| 6138 | const char *name; |
Willy Tarreau | c3b297d | 2023-03-22 17:01:32 +0100 | [diff] [blame] | 6139 | uint64_t bit, grp; |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6140 | void *curr, *next; |
| 6141 | } check_syms[] = { |
Willy Tarreau | 997ad15 | 2023-04-22 19:47:19 +0200 | [diff] [blame] | 6142 | /* openssl's libcrypto checks: group bits 0x1f */ |
| 6143 | { .name="OPENSSL_init", .bit = 0x0000000000000001, .grp = 0x000000000000001f, }, // openssl 1.0 / 1.1 / 3.0 |
| 6144 | { .name="OPENSSL_init_crypto", .bit = 0x0000000000000002, .grp = 0x000000000000001f, }, // openssl 1.1 / 3.0 |
| 6145 | { .name="ENGINE_init", .bit = 0x0000000000000004, .grp = 0x000000000000001f, }, // openssl 1.x / 3.x with engine |
| 6146 | { .name="EVP_CIPHER_CTX_init", .bit = 0x0000000000000008, .grp = 0x000000000000001f, }, // openssl 1.0 |
| 6147 | { .name="HMAC_Init", .bit = 0x0000000000000010, .grp = 0x000000000000001f, }, // openssl 1.x |
| 6148 | |
| 6149 | /* openssl's libssl checks: group bits 0x3e0 */ |
| 6150 | { .name="OPENSSL_init_ssl", .bit = 0x0000000000000020, .grp = 0x00000000000003e0, }, // openssl 1.1 / 3.0 |
| 6151 | { .name="SSL_library_init", .bit = 0x0000000000000040, .grp = 0x00000000000003e0, }, // openssl 1.x |
| 6152 | { .name="SSL_is_quic", .bit = 0x0000000000000080, .grp = 0x00000000000003e0, }, // quictls |
| 6153 | { .name="SSL_CTX_new_ex", .bit = 0x0000000000000100, .grp = 0x00000000000003e0, }, // openssl 3.x |
| 6154 | { .name="SSL_CTX_get0_security_ex_data", .bit = 0x0000000000000200, .grp = 0x00000000000003e0, }, // openssl 1.x / 3.x |
| 6155 | |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6156 | /* insert only above, 0 must be the last one */ |
| 6157 | { 0 }, |
| 6158 | }; |
| 6159 | const char *trace; |
Willy Tarreau | c3b297d | 2023-03-22 17:01:32 +0100 | [diff] [blame] | 6160 | uint64_t own_fp, lib_fp; // symbols fingerprints |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6161 | void *addr; |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6162 | void *ret; |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6163 | int sym = 0; |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6164 | |
| 6165 | if (!_dlopen) { |
| 6166 | _dlopen = get_sym_next_addr("dlopen"); |
| 6167 | if (!_dlopen || _dlopen == dlopen) { |
| 6168 | _dlopen = NULL; |
| 6169 | return NULL; |
| 6170 | } |
| 6171 | } |
| 6172 | |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6173 | /* save a few pointers to critical symbols. We keep a copy of both the |
| 6174 | * current and the next value, because we might already have replaced |
Willy Tarreau | 58912b8 | 2023-03-22 15:47:51 +0100 | [diff] [blame] | 6175 | * some of them in an inconsistent way (i.e. not all), and we're only |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6176 | * interested in verifying that a loaded library doesn't come with a |
Willy Tarreau | c3b297d | 2023-03-22 17:01:32 +0100 | [diff] [blame] | 6177 | * completely different definition that would be incompatible. We'll |
| 6178 | * keep a fingerprint of our own symbols. |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6179 | */ |
Willy Tarreau | c3b297d | 2023-03-22 17:01:32 +0100 | [diff] [blame] | 6180 | own_fp = 0; |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6181 | for (sym = 0; check_syms[sym].name; sym++) { |
| 6182 | check_syms[sym].curr = get_sym_curr_addr(check_syms[sym].name); |
| 6183 | check_syms[sym].next = get_sym_next_addr(check_syms[sym].name); |
Willy Tarreau | c3b297d | 2023-03-22 17:01:32 +0100 | [diff] [blame] | 6184 | if (check_syms[sym].curr || check_syms[sym].next) |
| 6185 | own_fp |= check_syms[sym].bit; |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6186 | } |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6187 | |
| 6188 | /* now open the requested lib */ |
| 6189 | ret = _dlopen(filename, flags); |
| 6190 | if (!ret) |
| 6191 | return ret; |
| 6192 | |
| 6193 | mark_tainted(TAINTED_SHARED_LIBS); |
| 6194 | |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6195 | /* and check that critical symbols didn't change */ |
Willy Tarreau | c3b297d | 2023-03-22 17:01:32 +0100 | [diff] [blame] | 6196 | lib_fp = 0; |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6197 | for (sym = 0; check_syms[sym].name; sym++) { |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6198 | addr = dlsym(ret, check_syms[sym].name); |
Willy Tarreau | c3b297d | 2023-03-22 17:01:32 +0100 | [diff] [blame] | 6199 | if (addr) |
| 6200 | lib_fp |= check_syms[sym].bit; |
| 6201 | } |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6202 | |
Willy Tarreau | c3b297d | 2023-03-22 17:01:32 +0100 | [diff] [blame] | 6203 | if (lib_fp != own_fp) { |
| 6204 | /* let's check what changed: */ |
| 6205 | uint64_t mask = 0; |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6206 | |
Willy Tarreau | c3b297d | 2023-03-22 17:01:32 +0100 | [diff] [blame] | 6207 | for (sym = 0; check_syms[sym].name; sym++) { |
| 6208 | mask = check_syms[sym].grp; |
| 6209 | |
| 6210 | /* new group of symbols. If they all appeared together |
| 6211 | * their use will be consistent. If none appears, it's |
| 6212 | * just that the lib doesn't use them. If some appear |
| 6213 | * or disappear, it means the lib relies on a different |
| 6214 | * dependency and will end up with a mix. |
| 6215 | */ |
| 6216 | if (!(own_fp & mask) || !(lib_fp & mask) || |
| 6217 | (own_fp & mask) == (lib_fp & mask)) |
| 6218 | continue; |
| 6219 | |
| 6220 | /* let's report a symbol that really changes */ |
| 6221 | if (!((own_fp ^ lib_fp) & check_syms[sym].bit)) |
| 6222 | continue; |
| 6223 | |
| 6224 | /* OK it's clear that this symbol was redefined */ |
| 6225 | mark_tainted(TAINTED_REDEFINITION); |
| 6226 | |
| 6227 | trace = hlua_show_current_location("\n "); |
| 6228 | ha_warning("dlopen(): shared library '%s' brings a different and inconsistent definition of symbol '%s'. The process cannot be trusted anymore!%s%s\n", |
| 6229 | filename, check_syms[sym].name, |
| 6230 | trace ? " Suspected call location: \n " : "", |
| 6231 | trace ? trace : ""); |
| 6232 | } |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6233 | } |
| 6234 | |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6235 | return ret; |
| 6236 | } |
| 6237 | #endif |
| 6238 | |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 6239 | static int init_tools_per_thread() |
| 6240 | { |
| 6241 | /* Let's make each thread start from a different position */ |
Willy Tarreau | e936aaf | 2024-03-01 16:17:47 +0100 | [diff] [blame] | 6242 | statistical_prng_state += ha_random32(); |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 6243 | if (!statistical_prng_state) |
| 6244 | statistical_prng_state++; |
| 6245 | return 1; |
| 6246 | } |
| 6247 | REGISTER_PER_THREAD_INIT(init_tools_per_thread); |
Willy Tarreau | c54e5ad | 2020-06-25 09:15:40 +0200 | [diff] [blame] | 6248 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6249 | /* |
| 6250 | * Local variables: |
| 6251 | * c-indent-level: 8 |
| 6252 | * c-basic-offset: 8 |
| 6253 | * End: |
| 6254 | */ |