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