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