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