Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * General purpose functions. |
| 3 | * |
Willy Tarreau | 348238b | 2010-01-18 15:05:57 +0100 | [diff] [blame] | 4 | * Copyright 2000-2010 Willy Tarreau <w@1wt.eu> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 13 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 14 | #define _GNU_SOURCE |
| 15 | #include <dlfcn.h> |
| 16 | #include <link.h> |
| 17 | #endif |
| 18 | |
devnexen@gmail.com | c4e5232 | 2021-08-17 12:55:49 +0100 | [diff] [blame] | 19 | #if defined(__FreeBSD__) |
| 20 | #include <elf.h> |
| 21 | #include <dlfcn.h> |
| 22 | extern void *__elf_aux_vector; |
| 23 | #endif |
| 24 | |
David Carlier | bd2cced | 2021-08-17 08:44:25 +0100 | [diff] [blame] | 25 | #if defined(__NetBSD__) |
| 26 | #include <sys/exec_elf.h> |
| 27 | #include <dlfcn.h> |
| 28 | #endif |
| 29 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 30 | #include <ctype.h> |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 31 | #include <errno.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 32 | #include <netdb.h> |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 33 | #include <stdarg.h> |
Willy Tarreau | dd2f85e | 2012-09-02 22:34:23 +0200 | [diff] [blame] | 34 | #include <stdio.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 35 | #include <stdlib.h> |
| 36 | #include <string.h> |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 37 | #include <time.h> |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 38 | #include <unistd.h> |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 39 | #include <sys/socket.h> |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 40 | #include <sys/stat.h> |
| 41 | #include <sys/types.h> |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 42 | #include <sys/un.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 43 | #include <netinet/in.h> |
| 44 | #include <arpa/inet.h> |
| 45 | |
David Carlier | 43a5685 | 2022-03-04 15:50:48 +0000 | [diff] [blame] | 46 | #if defined(__linux__) && defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16)) |
Willy Tarreau | 3005306 | 2020-08-20 16:39:14 +0200 | [diff] [blame] | 47 | #include <sys/auxv.h> |
| 48 | #endif |
| 49 | |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 50 | #include <import/eb32sctree.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 51 | #include <import/eb32tree.h> |
Willy Tarreau | 0dc9e6d | 2022-08-01 11:55:57 +0200 | [diff] [blame] | 52 | #include <import/ebmbtree.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 53 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 54 | #include <haproxy/api.h> |
Christopher Faulet | 6b0a0fb | 2022-04-04 11:29:28 +0200 | [diff] [blame] | 55 | #include <haproxy/applet.h> |
Willy Tarreau | c13ed53 | 2020-06-02 10:22:45 +0200 | [diff] [blame] | 56 | #include <haproxy/chunk.h> |
Willy Tarreau | 7c18b54 | 2020-06-11 09:23:02 +0200 | [diff] [blame] | 57 | #include <haproxy/dgram.h> |
Willy Tarreau | f268ee8 | 2020-06-04 17:05:57 +0200 | [diff] [blame] | 58 | #include <haproxy/global.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 59 | #include <haproxy/hlua.h> |
Willy Tarreau | 213e990 | 2020-06-04 14:58:24 +0200 | [diff] [blame] | 60 | #include <haproxy/listener.h> |
Willy Tarreau | 7a00efb | 2020-06-02 17:02:59 +0200 | [diff] [blame] | 61 | #include <haproxy/namespace.h> |
Christopher Faulet | 9553de7 | 2021-02-26 09:12:50 +0100 | [diff] [blame] | 62 | #include <haproxy/net_helper.h> |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 63 | #include <haproxy/protocol.h> |
Emeric Brun | c943799 | 2021-02-12 19:42:55 +0100 | [diff] [blame] | 64 | #include <haproxy/resolvers.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 65 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 66 | #include <haproxy/sock.h> |
Willy Tarreau | 209108d | 2020-06-04 20:30:20 +0200 | [diff] [blame] | 67 | #include <haproxy/ssl_sock.h> |
William Lallemand | 3aeb3f9 | 2021-08-21 23:59:56 +0200 | [diff] [blame] | 68 | #include <haproxy/ssl_utils.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 69 | #include <haproxy/stconn.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 70 | #include <haproxy/task.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 71 | #include <haproxy/tools.h> |
Willy Tarreau | 08093cc | 2022-11-24 08:09:12 +0100 | [diff] [blame] | 72 | #include <haproxy/xxhash.h> |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 73 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 74 | /* This macro returns false if the test __x is false. Many |
| 75 | * of the following parsing function must be abort the processing |
| 76 | * if it returns 0, so this macro is useful for writing light code. |
| 77 | */ |
| 78 | #define RET0_UNLESS(__x) do { if (!(__x)) return 0; } while (0) |
| 79 | |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 80 | /* Define the number of line of hash_word */ |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 81 | #define NB_L_HASH_WORD 15 |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 82 | |
Willy Tarreau | 08093cc | 2022-11-24 08:09:12 +0100 | [diff] [blame] | 83 | /* return the hash of a string and length for a given key. All keys are valid. */ |
| 84 | #define HA_ANON(key, str, len) (XXH32(str, len, key) & 0xFFFFFF) |
| 85 | |
Willy Tarreau | 56adcf2 | 2012-12-23 18:00:29 +0100 | [diff] [blame] | 86 | /* enough to store NB_ITOA_STR integers of : |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 87 | * 2^64-1 = 18446744073709551615 or |
| 88 | * -2^63 = -9223372036854775808 |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 89 | * |
| 90 | * The HTML version needs room for adding the 25 characters |
| 91 | * '<span class="rls"></span>' around digits at positions 3N+1 in order |
| 92 | * 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] | 93 | */ |
Christopher Faulet | 99bca65 | 2017-11-14 16:47:26 +0100 | [diff] [blame] | 94 | THREAD_LOCAL char itoa_str[NB_ITOA_STR][171]; |
| 95 | 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] | 96 | |
Willy Tarreau | 588297f | 2014-06-16 15:16:40 +0200 | [diff] [blame] | 97 | /* sometimes we'll need to quote strings (eg: in stats), and we don't expect |
| 98 | * to quote strings larger than a max configuration line. |
| 99 | */ |
Christopher Faulet | 99bca65 | 2017-11-14 16:47:26 +0100 | [diff] [blame] | 100 | THREAD_LOCAL char quoted_str[NB_QSTR][QSTR_SIZE + 1]; |
| 101 | THREAD_LOCAL int quoted_idx = 0; |
Willy Tarreau | 588297f | 2014-06-16 15:16:40 +0200 | [diff] [blame] | 102 | |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 103 | /* thread-local PRNG state. It's modified to start from a different sequence |
| 104 | * on all threads upon startup. It must not be used or anything beyond getting |
| 105 | * statistical values as it's 100% predictable. |
| 106 | */ |
| 107 | THREAD_LOCAL unsigned int statistical_prng_state = 2463534242U; |
| 108 | |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 109 | /* set to true if this is a static build */ |
| 110 | int build_is_static = 0; |
| 111 | |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 112 | /* A global static table to store hashed words */ |
| 113 | static THREAD_LOCAL char hash_word[NB_L_HASH_WORD][20]; |
| 114 | static THREAD_LOCAL int index_hash = 0; |
| 115 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 116 | /* |
William Lallemand | e7340ec | 2012-01-24 11:15:39 +0100 | [diff] [blame] | 117 | * unsigned long long ASCII representation |
| 118 | * |
| 119 | * return the last char '\0' or NULL if no enough |
| 120 | * space in dst |
| 121 | */ |
| 122 | char *ulltoa(unsigned long long n, char *dst, size_t size) |
| 123 | { |
| 124 | int i = 0; |
| 125 | char *res; |
| 126 | |
| 127 | switch(n) { |
| 128 | case 1ULL ... 9ULL: |
| 129 | i = 0; |
| 130 | break; |
| 131 | |
| 132 | case 10ULL ... 99ULL: |
| 133 | i = 1; |
| 134 | break; |
| 135 | |
| 136 | case 100ULL ... 999ULL: |
| 137 | i = 2; |
| 138 | break; |
| 139 | |
| 140 | case 1000ULL ... 9999ULL: |
| 141 | i = 3; |
| 142 | break; |
| 143 | |
| 144 | case 10000ULL ... 99999ULL: |
| 145 | i = 4; |
| 146 | break; |
| 147 | |
| 148 | case 100000ULL ... 999999ULL: |
| 149 | i = 5; |
| 150 | break; |
| 151 | |
| 152 | case 1000000ULL ... 9999999ULL: |
| 153 | i = 6; |
| 154 | break; |
| 155 | |
| 156 | case 10000000ULL ... 99999999ULL: |
| 157 | i = 7; |
| 158 | break; |
| 159 | |
| 160 | case 100000000ULL ... 999999999ULL: |
| 161 | i = 8; |
| 162 | break; |
| 163 | |
| 164 | case 1000000000ULL ... 9999999999ULL: |
| 165 | i = 9; |
| 166 | break; |
| 167 | |
| 168 | case 10000000000ULL ... 99999999999ULL: |
| 169 | i = 10; |
| 170 | break; |
| 171 | |
| 172 | case 100000000000ULL ... 999999999999ULL: |
| 173 | i = 11; |
| 174 | break; |
| 175 | |
| 176 | case 1000000000000ULL ... 9999999999999ULL: |
| 177 | i = 12; |
| 178 | break; |
| 179 | |
| 180 | case 10000000000000ULL ... 99999999999999ULL: |
| 181 | i = 13; |
| 182 | break; |
| 183 | |
| 184 | case 100000000000000ULL ... 999999999999999ULL: |
| 185 | i = 14; |
| 186 | break; |
| 187 | |
| 188 | case 1000000000000000ULL ... 9999999999999999ULL: |
| 189 | i = 15; |
| 190 | break; |
| 191 | |
| 192 | case 10000000000000000ULL ... 99999999999999999ULL: |
| 193 | i = 16; |
| 194 | break; |
| 195 | |
| 196 | case 100000000000000000ULL ... 999999999999999999ULL: |
| 197 | i = 17; |
| 198 | break; |
| 199 | |
| 200 | case 1000000000000000000ULL ... 9999999999999999999ULL: |
| 201 | i = 18; |
| 202 | break; |
| 203 | |
| 204 | case 10000000000000000000ULL ... ULLONG_MAX: |
| 205 | i = 19; |
| 206 | break; |
| 207 | } |
| 208 | if (i + 2 > size) // (i + 1) + '\0' |
| 209 | return NULL; // too long |
| 210 | res = dst + i + 1; |
| 211 | *res = '\0'; |
| 212 | for (; i >= 0; i--) { |
| 213 | dst[i] = n % 10ULL + '0'; |
| 214 | n /= 10ULL; |
| 215 | } |
| 216 | return res; |
| 217 | } |
| 218 | |
| 219 | /* |
| 220 | * unsigned long ASCII representation |
| 221 | * |
| 222 | * return the last char '\0' or NULL if no enough |
| 223 | * space in dst |
| 224 | */ |
| 225 | char *ultoa_o(unsigned long n, char *dst, size_t size) |
| 226 | { |
| 227 | int i = 0; |
| 228 | char *res; |
| 229 | |
| 230 | switch (n) { |
| 231 | case 0U ... 9UL: |
| 232 | i = 0; |
| 233 | break; |
| 234 | |
| 235 | case 10U ... 99UL: |
| 236 | i = 1; |
| 237 | break; |
| 238 | |
| 239 | case 100U ... 999UL: |
| 240 | i = 2; |
| 241 | break; |
| 242 | |
| 243 | case 1000U ... 9999UL: |
| 244 | i = 3; |
| 245 | break; |
| 246 | |
| 247 | case 10000U ... 99999UL: |
| 248 | i = 4; |
| 249 | break; |
| 250 | |
| 251 | case 100000U ... 999999UL: |
| 252 | i = 5; |
| 253 | break; |
| 254 | |
| 255 | case 1000000U ... 9999999UL: |
| 256 | i = 6; |
| 257 | break; |
| 258 | |
| 259 | case 10000000U ... 99999999UL: |
| 260 | i = 7; |
| 261 | break; |
| 262 | |
| 263 | case 100000000U ... 999999999UL: |
| 264 | i = 8; |
| 265 | break; |
| 266 | #if __WORDSIZE == 32 |
| 267 | |
| 268 | case 1000000000ULL ... ULONG_MAX: |
| 269 | i = 9; |
| 270 | break; |
| 271 | |
| 272 | #elif __WORDSIZE == 64 |
| 273 | |
| 274 | case 1000000000ULL ... 9999999999UL: |
| 275 | i = 9; |
| 276 | break; |
| 277 | |
| 278 | case 10000000000ULL ... 99999999999UL: |
| 279 | i = 10; |
| 280 | break; |
| 281 | |
| 282 | case 100000000000ULL ... 999999999999UL: |
| 283 | i = 11; |
| 284 | break; |
| 285 | |
| 286 | case 1000000000000ULL ... 9999999999999UL: |
| 287 | i = 12; |
| 288 | break; |
| 289 | |
| 290 | case 10000000000000ULL ... 99999999999999UL: |
| 291 | i = 13; |
| 292 | break; |
| 293 | |
| 294 | case 100000000000000ULL ... 999999999999999UL: |
| 295 | i = 14; |
| 296 | break; |
| 297 | |
| 298 | case 1000000000000000ULL ... 9999999999999999UL: |
| 299 | i = 15; |
| 300 | break; |
| 301 | |
| 302 | case 10000000000000000ULL ... 99999999999999999UL: |
| 303 | i = 16; |
| 304 | break; |
| 305 | |
| 306 | case 100000000000000000ULL ... 999999999999999999UL: |
| 307 | i = 17; |
| 308 | break; |
| 309 | |
| 310 | case 1000000000000000000ULL ... 9999999999999999999UL: |
| 311 | i = 18; |
| 312 | break; |
| 313 | |
| 314 | case 10000000000000000000ULL ... ULONG_MAX: |
| 315 | i = 19; |
| 316 | break; |
| 317 | |
| 318 | #endif |
| 319 | } |
| 320 | if (i + 2 > size) // (i + 1) + '\0' |
| 321 | return NULL; // too long |
| 322 | res = dst + i + 1; |
| 323 | *res = '\0'; |
| 324 | for (; i >= 0; i--) { |
| 325 | dst[i] = n % 10U + '0'; |
| 326 | n /= 10U; |
| 327 | } |
| 328 | return res; |
| 329 | } |
| 330 | |
| 331 | /* |
| 332 | * signed long ASCII representation |
| 333 | * |
| 334 | * return the last char '\0' or NULL if no enough |
| 335 | * space in dst |
| 336 | */ |
| 337 | char *ltoa_o(long int n, char *dst, size_t size) |
| 338 | { |
| 339 | char *pos = dst; |
| 340 | |
| 341 | if (n < 0) { |
| 342 | if (size < 3) |
| 343 | return NULL; // min size is '-' + digit + '\0' but another test in ultoa |
| 344 | *pos = '-'; |
| 345 | pos++; |
| 346 | dst = ultoa_o(-n, pos, size - 1); |
| 347 | } else { |
| 348 | dst = ultoa_o(n, dst, size); |
| 349 | } |
| 350 | return dst; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * signed long long ASCII representation |
| 355 | * |
| 356 | * return the last char '\0' or NULL if no enough |
| 357 | * space in dst |
| 358 | */ |
| 359 | char *lltoa(long long n, char *dst, size_t size) |
| 360 | { |
| 361 | char *pos = dst; |
| 362 | |
| 363 | if (n < 0) { |
| 364 | if (size < 3) |
| 365 | return NULL; // min size is '-' + digit + '\0' but another test in ulltoa |
| 366 | *pos = '-'; |
| 367 | pos++; |
| 368 | dst = ulltoa(-n, pos, size - 1); |
| 369 | } else { |
| 370 | dst = ulltoa(n, dst, size); |
| 371 | } |
| 372 | return dst; |
| 373 | } |
| 374 | |
| 375 | /* |
| 376 | * write a ascii representation of a unsigned into dst, |
| 377 | * return a pointer to the last character |
| 378 | * Pad the ascii representation with '0', using size. |
| 379 | */ |
| 380 | char *utoa_pad(unsigned int n, char *dst, size_t size) |
| 381 | { |
| 382 | int i = 0; |
| 383 | char *ret; |
| 384 | |
| 385 | switch(n) { |
| 386 | case 0U ... 9U: |
| 387 | i = 0; |
| 388 | break; |
| 389 | |
| 390 | case 10U ... 99U: |
| 391 | i = 1; |
| 392 | break; |
| 393 | |
| 394 | case 100U ... 999U: |
| 395 | i = 2; |
| 396 | break; |
| 397 | |
| 398 | case 1000U ... 9999U: |
| 399 | i = 3; |
| 400 | break; |
| 401 | |
| 402 | case 10000U ... 99999U: |
| 403 | i = 4; |
| 404 | break; |
| 405 | |
| 406 | case 100000U ... 999999U: |
| 407 | i = 5; |
| 408 | break; |
| 409 | |
| 410 | case 1000000U ... 9999999U: |
| 411 | i = 6; |
| 412 | break; |
| 413 | |
| 414 | case 10000000U ... 99999999U: |
| 415 | i = 7; |
| 416 | break; |
| 417 | |
| 418 | case 100000000U ... 999999999U: |
| 419 | i = 8; |
| 420 | break; |
| 421 | |
| 422 | case 1000000000U ... 4294967295U: |
| 423 | i = 9; |
| 424 | break; |
| 425 | } |
| 426 | if (i + 2 > size) // (i + 1) + '\0' |
| 427 | return NULL; // too long |
Aurelien DARRAGON | e3177af | 2022-11-22 11:42:07 +0100 | [diff] [blame] | 428 | i = size - 2; // padding - '\0' |
William Lallemand | e7340ec | 2012-01-24 11:15:39 +0100 | [diff] [blame] | 429 | |
| 430 | ret = dst + i + 1; |
| 431 | *ret = '\0'; |
| 432 | for (; i >= 0; i--) { |
| 433 | dst[i] = n % 10U + '0'; |
| 434 | n /= 10U; |
| 435 | } |
| 436 | return ret; |
| 437 | } |
| 438 | |
| 439 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 440 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 441 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 442 | * (excluding the terminating zero). |
| 443 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 444 | * long, uses only registers, and consumes only 4 cycles per char. |
| 445 | */ |
| 446 | int strlcpy2(char *dst, const char *src, int size) |
| 447 | { |
| 448 | char *orig = dst; |
| 449 | if (size) { |
| 450 | while (--size && (*dst = *src)) { |
| 451 | src++; dst++; |
| 452 | } |
| 453 | *dst = 0; |
| 454 | } |
| 455 | return dst - orig; |
| 456 | } |
| 457 | |
| 458 | /* |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 459 | * This function simply returns a locally allocated string containing |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 460 | * the ascii representation for number 'n' in decimal. |
| 461 | */ |
Emeric Brun | 3a7fce5 | 2010-01-04 14:54:38 +0100 | [diff] [blame] | 462 | char *ultoa_r(unsigned long n, char *buffer, int size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 463 | { |
| 464 | char *pos; |
| 465 | |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 466 | pos = buffer + size - 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 467 | *pos-- = '\0'; |
| 468 | |
| 469 | do { |
| 470 | *pos-- = '0' + n % 10; |
| 471 | n /= 10; |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 472 | } while (n && pos >= buffer); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 473 | return pos + 1; |
| 474 | } |
| 475 | |
Willy Tarreau | 91092e5 | 2007-10-25 16:58:42 +0200 | [diff] [blame] | 476 | /* |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 477 | * This function simply returns a locally allocated string containing |
Thierry FOURNIER | 763a5d8 | 2015-07-06 23:09:52 +0200 | [diff] [blame] | 478 | * the ascii representation for number 'n' in decimal. |
| 479 | */ |
| 480 | char *lltoa_r(long long int in, char *buffer, int size) |
| 481 | { |
| 482 | char *pos; |
| 483 | int neg = 0; |
| 484 | unsigned long long int n; |
| 485 | |
| 486 | pos = buffer + size - 1; |
| 487 | *pos-- = '\0'; |
| 488 | |
| 489 | if (in < 0) { |
| 490 | neg = 1; |
| 491 | n = -in; |
| 492 | } |
| 493 | else |
| 494 | n = in; |
| 495 | |
| 496 | do { |
| 497 | *pos-- = '0' + n % 10; |
| 498 | n /= 10; |
| 499 | } while (n && pos >= buffer); |
| 500 | if (neg && pos > buffer) |
| 501 | *pos-- = '-'; |
| 502 | return pos + 1; |
| 503 | } |
| 504 | |
| 505 | /* |
| 506 | * This function simply returns a locally allocated string containing |
Thierry FOURNIER | 1480bd8 | 2015-06-06 19:14:59 +0200 | [diff] [blame] | 507 | * the ascii representation for signed number 'n' in decimal. |
| 508 | */ |
| 509 | char *sltoa_r(long n, char *buffer, int size) |
| 510 | { |
| 511 | char *pos; |
| 512 | |
| 513 | if (n >= 0) |
| 514 | return ultoa_r(n, buffer, size); |
| 515 | |
| 516 | pos = ultoa_r(-n, buffer + 1, size - 1) - 1; |
| 517 | *pos = '-'; |
| 518 | return pos; |
| 519 | } |
| 520 | |
| 521 | /* |
| 522 | * This function simply returns a locally allocated string containing |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 523 | * the ascii representation for number 'n' in decimal, formatted for |
| 524 | * HTML output with tags to create visual grouping by 3 digits. The |
| 525 | * output needs to support at least 171 characters. |
| 526 | */ |
| 527 | const char *ulltoh_r(unsigned long long n, char *buffer, int size) |
| 528 | { |
| 529 | char *start; |
| 530 | int digit = 0; |
| 531 | |
| 532 | start = buffer + size; |
| 533 | *--start = '\0'; |
| 534 | |
| 535 | do { |
| 536 | if (digit == 3 && start >= buffer + 7) |
| 537 | memcpy(start -= 7, "</span>", 7); |
| 538 | |
| 539 | if (start >= buffer + 1) { |
| 540 | *--start = '0' + n % 10; |
| 541 | n /= 10; |
| 542 | } |
| 543 | |
| 544 | if (digit == 3 && start >= buffer + 18) |
| 545 | memcpy(start -= 18, "<span class=\"rls\">", 18); |
| 546 | |
| 547 | if (digit++ == 3) |
| 548 | digit = 1; |
| 549 | } while (n && start > buffer); |
| 550 | return start; |
| 551 | } |
| 552 | |
| 553 | /* |
Willy Tarreau | 91092e5 | 2007-10-25 16:58:42 +0200 | [diff] [blame] | 554 | * This function simply returns a locally allocated string containing the ascii |
| 555 | * representation for number 'n' in decimal, unless n is 0 in which case it |
| 556 | * returns the alternate string (or an empty string if the alternate string is |
| 557 | * NULL). It use is intended for limits reported in reports, where it's |
| 558 | * desirable not to display anything if there is no limit. Warning! it shares |
| 559 | * the same vector as ultoa_r(). |
| 560 | */ |
| 561 | const char *limit_r(unsigned long n, char *buffer, int size, const char *alt) |
| 562 | { |
| 563 | return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : ""); |
| 564 | } |
| 565 | |
Willy Tarreau | 56d1d8d | 2021-05-08 10:28:53 +0200 | [diff] [blame] | 566 | /* Trims the first "%f" float in a string to its minimum number of digits after |
| 567 | * the decimal point by trimming trailing zeroes, even dropping the decimal |
| 568 | * point if not needed. The string is in <buffer> of length <len>, and the |
| 569 | * number is expected to start at or after position <num_start> (the first |
| 570 | * point appearing there is considered). A NUL character is always placed at |
| 571 | * the end if some trimming occurs. The new buffer length is returned. |
| 572 | */ |
| 573 | size_t flt_trim(char *buffer, size_t num_start, size_t len) |
| 574 | { |
| 575 | char *end = buffer + len; |
| 576 | char *p = buffer + num_start; |
| 577 | char *trim; |
| 578 | |
| 579 | do { |
| 580 | if (p >= end) |
| 581 | return len; |
| 582 | trim = p++; |
| 583 | } while (*trim != '.'); |
| 584 | |
| 585 | /* For now <trim> is on the decimal point. Let's look for any other |
| 586 | * meaningful digit after it. |
| 587 | */ |
| 588 | while (p < end) { |
| 589 | if (*p++ != '0') |
| 590 | trim = p; |
| 591 | } |
| 592 | |
| 593 | if (trim < end) |
| 594 | *trim = 0; |
| 595 | |
| 596 | return trim - buffer; |
| 597 | } |
| 598 | |
Willy Tarreau | ae03d26 | 2021-05-08 07:35:00 +0200 | [diff] [blame] | 599 | /* |
| 600 | * This function simply returns a locally allocated string containing |
| 601 | * the ascii representation for number 'n' in decimal with useless trailing |
| 602 | * zeroes trimmed. |
| 603 | */ |
| 604 | char *ftoa_r(double n, char *buffer, int size) |
| 605 | { |
| 606 | flt_trim(buffer, 0, snprintf(buffer, size, "%f", n)); |
| 607 | return buffer; |
| 608 | } |
| 609 | |
Willy Tarreau | 588297f | 2014-06-16 15:16:40 +0200 | [diff] [blame] | 610 | /* returns a locally allocated string containing the quoted encoding of the |
| 611 | * input string. The output may be truncated to QSTR_SIZE chars, but it is |
| 612 | * guaranteed that the string will always be properly terminated. Quotes are |
| 613 | * encoded by doubling them as is commonly done in CSV files. QSTR_SIZE must |
| 614 | * always be at least 4 chars. |
| 615 | */ |
| 616 | const char *qstr(const char *str) |
| 617 | { |
| 618 | char *ret = quoted_str[quoted_idx]; |
| 619 | char *p, *end; |
| 620 | |
| 621 | if (++quoted_idx >= NB_QSTR) |
| 622 | quoted_idx = 0; |
| 623 | |
| 624 | p = ret; |
| 625 | end = ret + QSTR_SIZE; |
| 626 | |
| 627 | *p++ = '"'; |
| 628 | |
| 629 | /* always keep 3 chars to support passing "" and the ending " */ |
| 630 | while (*str && p < end - 3) { |
| 631 | if (*str == '"') { |
| 632 | *p++ = '"'; |
| 633 | *p++ = '"'; |
| 634 | } |
| 635 | else |
| 636 | *p++ = *str; |
| 637 | str++; |
| 638 | } |
| 639 | *p++ = '"'; |
| 640 | return ret; |
| 641 | } |
| 642 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 643 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 644 | * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero. |
| 645 | * |
| 646 | * It looks like this one would be a good candidate for inlining, but this is |
| 647 | * not interesting because it around 35 bytes long and often called multiple |
| 648 | * times within the same function. |
| 649 | */ |
| 650 | int ishex(char s) |
| 651 | { |
| 652 | s -= '0'; |
| 653 | if ((unsigned char)s <= 9) |
| 654 | return 1; |
| 655 | s -= 'A' - '0'; |
| 656 | if ((unsigned char)s <= 5) |
| 657 | return 1; |
| 658 | s -= 'a' - 'A'; |
| 659 | if ((unsigned char)s <= 5) |
| 660 | return 1; |
| 661 | return 0; |
| 662 | } |
| 663 | |
Willy Tarreau | 3ca1a88 | 2015-01-15 18:43:49 +0100 | [diff] [blame] | 664 | /* rounds <i> down to the closest value having max 2 digits */ |
| 665 | unsigned int round_2dig(unsigned int i) |
| 666 | { |
| 667 | unsigned int mul = 1; |
| 668 | |
| 669 | while (i >= 100) { |
| 670 | i /= 10; |
| 671 | mul *= 10; |
| 672 | } |
| 673 | return i * mul; |
| 674 | } |
| 675 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 676 | /* |
| 677 | * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an |
| 678 | * invalid character is found, a pointer to it is returned. If everything is |
| 679 | * fine, NULL is returned. |
| 680 | */ |
| 681 | const char *invalid_char(const char *name) |
| 682 | { |
| 683 | if (!*name) |
| 684 | return name; |
| 685 | |
| 686 | while (*name) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 687 | if (!isalnum((unsigned char)*name) && *name != '.' && *name != ':' && |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 688 | *name != '_' && *name != '-') |
| 689 | return name; |
| 690 | name++; |
| 691 | } |
| 692 | return NULL; |
| 693 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 694 | |
| 695 | /* |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 696 | * Checks <name> for invalid characters. Valid chars are [_.-] and those |
| 697 | * accepted by <f> function. |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 698 | * If an invalid character is found, a pointer to it is returned. |
| 699 | * If everything is fine, NULL is returned. |
| 700 | */ |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 701 | 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] | 702 | |
| 703 | if (!*name) |
| 704 | return name; |
| 705 | |
| 706 | while (*name) { |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 707 | if (!f((unsigned char)*name) && *name != '.' && |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 708 | *name != '_' && *name != '-') |
| 709 | return name; |
| 710 | |
| 711 | name++; |
| 712 | } |
| 713 | |
| 714 | return NULL; |
| 715 | } |
| 716 | |
| 717 | /* |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 718 | * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_.-]. |
| 719 | * If an invalid character is found, a pointer to it is returned. |
| 720 | * If everything is fine, NULL is returned. |
| 721 | */ |
| 722 | const char *invalid_domainchar(const char *name) { |
| 723 | return __invalid_char(name, isalnum); |
| 724 | } |
| 725 | |
| 726 | /* |
| 727 | * Checks <name> for invalid characters. Valid chars are [A-Za-z_.-]. |
| 728 | * If an invalid character is found, a pointer to it is returned. |
| 729 | * If everything is fine, NULL is returned. |
| 730 | */ |
| 731 | const char *invalid_prefix_char(const char *name) { |
Thierry Fournier | f7b7c3e | 2018-03-26 11:54:39 +0200 | [diff] [blame] | 732 | return __invalid_char(name, isalnum); |
Frédéric Lécaille | b82f742 | 2017-04-13 18:24:23 +0200 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | /* |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 736 | * converts <str> to a struct sockaddr_storage* provided by the caller. The |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 737 | * caller must have zeroed <sa> first, and may have set sa->ss_family to force |
| 738 | * parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then |
| 739 | * the function tries to guess the address family from the syntax. If the |
| 740 | * 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] | 741 | * string is assumed to contain only an address, no port. The address can be a |
| 742 | * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to |
| 743 | * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved. |
| 744 | * The return address will only have the address family and the address set, |
| 745 | * 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] | 746 | * The IPv6 '::' address is IN6ADDR_ANY. If <resolve> is non-zero, the hostname |
| 747 | * is resolved, otherwise only IP addresses are resolved, and anything else |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 748 | * returns NULL. If the address contains a port, this one is preserved. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 749 | */ |
Thierry FOURNIER | 58639a0 | 2014-11-25 12:02:25 +0100 | [diff] [blame] | 750 | 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] | 751 | { |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 752 | struct hostent *he; |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 753 | /* max IPv6 length, including brackets and terminating NULL */ |
| 754 | char tmpip[48]; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 755 | int port = get_host_port(sa); |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 756 | |
| 757 | /* check IPv6 with square brackets */ |
| 758 | if (str[0] == '[') { |
| 759 | size_t iplength = strlen(str); |
| 760 | |
| 761 | if (iplength < 4) { |
| 762 | /* minimal size is 4 when using brackets "[::]" */ |
| 763 | goto fail; |
| 764 | } |
| 765 | else if (iplength >= sizeof(tmpip)) { |
| 766 | /* IPv6 literal can not be larger than tmpip */ |
| 767 | goto fail; |
| 768 | } |
| 769 | else { |
| 770 | if (str[iplength - 1] != ']') { |
| 771 | /* if address started with bracket, it should end with bracket */ |
| 772 | goto fail; |
| 773 | } |
| 774 | else { |
| 775 | memcpy(tmpip, str + 1, iplength - 2); |
| 776 | tmpip[iplength - 2] = '\0'; |
| 777 | str = tmpip; |
| 778 | } |
| 779 | } |
| 780 | } |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 781 | |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 782 | /* Any IPv6 address */ |
| 783 | if (str[0] == ':' && str[1] == ':' && !str[2]) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 784 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 785 | sa->ss_family = AF_INET6; |
| 786 | else if (sa->ss_family != AF_INET6) |
| 787 | goto fail; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 788 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 789 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 790 | } |
| 791 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 792 | /* Any address for the family, defaults to IPv4 */ |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 793 | if (!str[0] || (str[0] == '*' && !str[1])) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 794 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 795 | sa->ss_family = AF_INET; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 796 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 797 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 798 | } |
| 799 | |
| 800 | /* check for IPv6 first */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 801 | if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET6) && |
| 802 | inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)sa)->sin6_addr)) { |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 803 | sa->ss_family = AF_INET6; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 804 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 805 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 806 | } |
| 807 | |
| 808 | /* then check for IPv4 */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 809 | if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET) && |
| 810 | inet_pton(AF_INET, str, &((struct sockaddr_in *)sa)->sin_addr)) { |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 811 | sa->ss_family = AF_INET; |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 812 | set_host_port(sa, port); |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 813 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 814 | } |
| 815 | |
Thierry FOURNIER | 58639a0 | 2014-11-25 12:02:25 +0100 | [diff] [blame] | 816 | if (!resolve) |
| 817 | return NULL; |
| 818 | |
Emeric Brun | d30e9a1 | 2020-12-23 18:49:16 +0100 | [diff] [blame] | 819 | if (!resolv_hostname_validation(str, NULL)) |
Baptiste Assmann | a68ca96 | 2015-04-14 01:15:08 +0200 | [diff] [blame] | 820 | return NULL; |
| 821 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 822 | #ifdef USE_GETADDRINFO |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 823 | if (global.tune.options & GTUNE_USE_GAI) { |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 824 | struct addrinfo hints, *result; |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 825 | int success = 0; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 826 | |
| 827 | memset(&result, 0, sizeof(result)); |
| 828 | memset(&hints, 0, sizeof(hints)); |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 829 | hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 830 | hints.ai_socktype = SOCK_DGRAM; |
Dmitry Sivachenko | eab7f39 | 2015-10-02 01:01:58 +0200 | [diff] [blame] | 831 | hints.ai_flags = 0; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 832 | hints.ai_protocol = 0; |
| 833 | |
| 834 | if (getaddrinfo(str, NULL, &hints, &result) == 0) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 835 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 836 | sa->ss_family = result->ai_family; |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 837 | else if (sa->ss_family != result->ai_family) { |
| 838 | freeaddrinfo(result); |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 839 | goto fail; |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 840 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 841 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 842 | switch (result->ai_family) { |
| 843 | case AF_INET: |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 844 | memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen); |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 845 | set_host_port(sa, port); |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 846 | success = 1; |
| 847 | break; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 848 | case AF_INET6: |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 849 | memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen); |
Willy Tarreau | ecde7df | 2016-11-02 22:37:03 +0100 | [diff] [blame] | 850 | set_host_port(sa, port); |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 851 | success = 1; |
| 852 | break; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Sean Carey | 58ea039 | 2013-02-15 23:39:18 +0100 | [diff] [blame] | 856 | if (result) |
| 857 | freeaddrinfo(result); |
Tim Duesterhus | 7d58b4d | 2018-01-21 22:11:17 +0100 | [diff] [blame] | 858 | |
| 859 | if (success) |
| 860 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 861 | } |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 862 | #endif |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 863 | /* try to resolve an IPv4/IPv6 hostname */ |
| 864 | he = gethostbyname(str); |
| 865 | if (he) { |
| 866 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 867 | sa->ss_family = he->h_addrtype; |
| 868 | else if (sa->ss_family != he->h_addrtype) |
| 869 | goto fail; |
| 870 | |
| 871 | switch (sa->ss_family) { |
| 872 | case AF_INET: |
| 873 | ((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] | 874 | set_host_port(sa, port); |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 875 | return sa; |
| 876 | case AF_INET6: |
| 877 | ((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] | 878 | set_host_port(sa, port); |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 879 | return sa; |
| 880 | } |
| 881 | } |
| 882 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 883 | /* unsupported address family */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 884 | fail: |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 885 | return NULL; |
| 886 | } |
| 887 | |
| 888 | /* |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 889 | * Converts <str> to a locally allocated struct sockaddr_storage *, and a port |
| 890 | * range or offset consisting in two integers that the caller will have to |
| 891 | * check to find the relevant input format. The following format are supported : |
| 892 | * |
| 893 | * String format | address | port | low | high |
| 894 | * addr | <addr> | 0 | 0 | 0 |
| 895 | * addr: | <addr> | 0 | 0 | 0 |
| 896 | * addr:port | <addr> | <port> | <port> | <port> |
| 897 | * addr:pl-ph | <addr> | <pl> | <pl> | <ph> |
| 898 | * addr:+port | <addr> | <port> | 0 | <port> |
| 899 | * addr:-port | <addr> |-<port> | <port> | 0 |
| 900 | * |
| 901 | * The detection of a port range or increment by the caller is made by |
| 902 | * comparing <low> and <high>. If both are equal, then port 0 means no port |
| 903 | * was specified. The caller may pass NULL for <low> and <high> if it is not |
| 904 | * interested in retrieving port ranges. |
| 905 | * |
| 906 | * Note that <addr> above may also be : |
| 907 | * - empty ("") => family will be AF_INET and address will be INADDR_ANY |
| 908 | * - "*" => family will be AF_INET and address will be INADDR_ANY |
| 909 | * - "::" => family will be AF_INET6 and address will be IN6ADDR_ANY |
| 910 | * - a host name => family and address will depend on host name resolving. |
| 911 | * |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 912 | * A prefix may be passed in before the address above to force the family : |
| 913 | * - "ipv4@" => force address to resolve as IPv4 and fail if not possible. |
| 914 | * - "ipv6@" => force address to resolve as IPv6 and fail if not possible. |
| 915 | * - "unix@" => force address to be a path to a UNIX socket even if the |
| 916 | * path does not start with a '/' |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 917 | * - 'abns@' -> force address to belong to the abstract namespace (Linux |
| 918 | * only). These sockets are just like Unix sockets but without |
| 919 | * the need for an underlying file system. The address is a |
| 920 | * string. Technically it's like a Unix socket with a zero in |
| 921 | * the first byte of the address. |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 922 | * - "fd@" => an integer must follow, and is a file descriptor number. |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 923 | * |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 924 | * IPv6 addresses can be declared with or without square brackets. When using |
| 925 | * square brackets for IPv6 addresses, the port separator (colon) is optional. |
| 926 | * If not using square brackets, and in order to avoid any ambiguity with |
| 927 | * IPv6 addresses, the last colon ':' is mandatory even when no port is specified. |
| 928 | * NULL is returned if the address cannot be parsed. The <low> and <high> ports |
| 929 | * are always initialized if non-null, even for non-IP families. |
Willy Tarreau | d393a62 | 2013-03-04 18:22:00 +0100 | [diff] [blame] | 930 | * |
| 931 | * If <pfx> is non-null, it is used as a string prefix before any path-based |
| 932 | * address (typically the path to a unix socket). |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 933 | * |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 934 | * if <fqdn> is non-null, it will be filled with : |
| 935 | * - a pointer to the FQDN of the server name to resolve if there's one, and |
| 936 | * that the caller will have to free(), |
| 937 | * - NULL if there was an explicit address that doesn't require resolution. |
| 938 | * |
Willy Tarreau | cd3a5591 | 2020-09-04 15:30:46 +0200 | [diff] [blame] | 939 | * Hostnames are only resolved if <opts> has PA_O_RESOLVE. Otherwise <fqdn> is |
| 940 | * still honored so it is possible for the caller to know whether a resolution |
| 941 | * failed by clearing this flag and checking if <fqdn> was filled, indicating |
| 942 | * the need for a resolution. |
Thierry FOURNIER | 7fe3be7 | 2015-09-26 20:03:36 +0200 | [diff] [blame] | 943 | * |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 944 | * 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] | 945 | * the address when cast to sockaddr_in and the address family is |
| 946 | * AF_CUST_EXISTING_FD. |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 947 | * |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 948 | * The matching protocol will be set into <proto> if non-null. |
| 949 | * |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 950 | * Any known file descriptor is also assigned to <fd> if non-null, otherwise it |
| 951 | * is forced to -1. |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 952 | */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 953 | struct sockaddr_storage *str2sa_range(const char *str, int *port, int *low, int *high, int *fd, |
| 954 | struct protocol **proto, char **err, |
| 955 | const char *pfx, char **fqdn, unsigned int opts) |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 956 | { |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 957 | static THREAD_LOCAL struct sockaddr_storage ss; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 958 | struct sockaddr_storage *ret = NULL; |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 959 | struct protocol *new_proto = NULL; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 960 | char *back, *str2; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 961 | char *port1, *port2; |
| 962 | int portl, porth, porta; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 963 | int abstract = 0; |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 964 | int new_fd = -1; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 965 | enum proto_type proto_type; |
| 966 | int ctrl_type; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 967 | |
| 968 | portl = porth = porta = 0; |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 969 | if (fqdn) |
| 970 | *fqdn = NULL; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 971 | |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 972 | str2 = back = env_expand(strdup(str)); |
Willy Tarreau | df350f1 | 2013-03-01 20:22:54 +0100 | [diff] [blame] | 973 | if (str2 == NULL) { |
| 974 | memprintf(err, "out of memory in '%s'\n", __FUNCTION__); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 975 | goto out; |
Willy Tarreau | df350f1 | 2013-03-01 20:22:54 +0100 | [diff] [blame] | 976 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 977 | |
Willy Tarreau | 9f69f46 | 2015-09-08 16:01:25 +0200 | [diff] [blame] | 978 | if (!*str2) { |
| 979 | memprintf(err, "'%s' resolves to an empty address (environment variable missing?)\n", str); |
| 980 | goto out; |
| 981 | } |
| 982 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 983 | memset(&ss, 0, sizeof(ss)); |
| 984 | |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 985 | /* prepare the default socket types */ |
Willy Tarreau | f23b1bc | 2021-03-23 18:36:37 +0100 | [diff] [blame] | 986 | if ((opts & (PA_O_STREAM|PA_O_DGRAM)) == PA_O_DGRAM || |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 987 | ((opts & (PA_O_STREAM|PA_O_DGRAM)) == (PA_O_DGRAM|PA_O_STREAM) && (opts & PA_O_DEFAULT_DGRAM))) { |
| 988 | proto_type = PROTO_TYPE_DGRAM; |
| 989 | ctrl_type = SOCK_DGRAM; |
| 990 | } else { |
| 991 | proto_type = PROTO_TYPE_STREAM; |
| 992 | ctrl_type = SOCK_STREAM; |
| 993 | } |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 994 | |
| 995 | if (strncmp(str2, "stream+", 7) == 0) { |
| 996 | str2 += 7; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 997 | proto_type = PROTO_TYPE_STREAM; |
| 998 | ctrl_type = SOCK_STREAM; |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 999 | } |
| 1000 | else if (strncmp(str2, "dgram+", 6) == 0) { |
| 1001 | str2 += 6; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1002 | proto_type = PROTO_TYPE_DGRAM; |
| 1003 | ctrl_type = SOCK_DGRAM; |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1004 | } |
Willy Tarreau | 40725a4 | 2023-01-16 13:55:27 +0100 | [diff] [blame] | 1005 | else if (strncmp(str2, "quic+", 5) == 0) { |
| 1006 | str2 += 5; |
| 1007 | proto_type = PROTO_TYPE_DGRAM; |
| 1008 | ctrl_type = SOCK_STREAM; |
| 1009 | } |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1010 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1011 | if (strncmp(str2, "unix@", 5) == 0) { |
| 1012 | str2 += 5; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 1013 | abstract = 0; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1014 | ss.ss_family = AF_UNIX; |
| 1015 | } |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1016 | else if (strncmp(str2, "uxdg@", 5) == 0) { |
| 1017 | str2 += 5; |
| 1018 | abstract = 0; |
| 1019 | ss.ss_family = AF_UNIX; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1020 | proto_type = PROTO_TYPE_DGRAM; |
| 1021 | ctrl_type = SOCK_DGRAM; |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1022 | } |
| 1023 | else if (strncmp(str2, "uxst@", 5) == 0) { |
| 1024 | str2 += 5; |
| 1025 | abstract = 0; |
| 1026 | ss.ss_family = AF_UNIX; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1027 | proto_type = PROTO_TYPE_STREAM; |
| 1028 | ctrl_type = SOCK_STREAM; |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1029 | } |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 1030 | else if (strncmp(str2, "abns@", 5) == 0) { |
| 1031 | str2 += 5; |
| 1032 | abstract = 1; |
| 1033 | ss.ss_family = AF_UNIX; |
| 1034 | } |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1035 | else if (strncmp(str2, "ip@", 3) == 0) { |
| 1036 | str2 += 3; |
| 1037 | ss.ss_family = AF_UNSPEC; |
| 1038 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1039 | else if (strncmp(str2, "ipv4@", 5) == 0) { |
| 1040 | str2 += 5; |
| 1041 | ss.ss_family = AF_INET; |
| 1042 | } |
| 1043 | else if (strncmp(str2, "ipv6@", 5) == 0) { |
| 1044 | str2 += 5; |
| 1045 | ss.ss_family = AF_INET6; |
| 1046 | } |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1047 | else if (strncmp(str2, "tcp4@", 5) == 0) { |
| 1048 | str2 += 5; |
| 1049 | ss.ss_family = AF_INET; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1050 | proto_type = PROTO_TYPE_STREAM; |
| 1051 | ctrl_type = SOCK_STREAM; |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1052 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1053 | else if (strncmp(str2, "udp4@", 5) == 0) { |
| 1054 | str2 += 5; |
| 1055 | ss.ss_family = AF_INET; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1056 | proto_type = PROTO_TYPE_DGRAM; |
| 1057 | ctrl_type = SOCK_DGRAM; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1058 | } |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1059 | else if (strncmp(str2, "tcp6@", 5) == 0) { |
| 1060 | str2 += 5; |
| 1061 | ss.ss_family = AF_INET6; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1062 | proto_type = PROTO_TYPE_STREAM; |
| 1063 | ctrl_type = SOCK_STREAM; |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1064 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1065 | else if (strncmp(str2, "udp6@", 5) == 0) { |
| 1066 | str2 += 5; |
| 1067 | ss.ss_family = AF_INET6; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1068 | proto_type = PROTO_TYPE_DGRAM; |
| 1069 | ctrl_type = SOCK_DGRAM; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1070 | } |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1071 | else if (strncmp(str2, "tcp@", 4) == 0) { |
| 1072 | str2 += 4; |
| 1073 | ss.ss_family = AF_UNSPEC; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1074 | proto_type = PROTO_TYPE_STREAM; |
| 1075 | ctrl_type = SOCK_STREAM; |
Emeric Brun | ce325c4 | 2021-04-02 17:05:09 +0200 | [diff] [blame] | 1076 | } |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1077 | else if (strncmp(str2, "udp@", 4) == 0) { |
| 1078 | str2 += 4; |
| 1079 | ss.ss_family = AF_UNSPEC; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1080 | proto_type = PROTO_TYPE_DGRAM; |
| 1081 | ctrl_type = SOCK_DGRAM; |
Emeric Brun | 3835c0d | 2020-07-07 09:46:09 +0200 | [diff] [blame] | 1082 | } |
Frédéric Lécaille | 10caf65 | 2020-11-23 11:36:57 +0100 | [diff] [blame] | 1083 | else if (strncmp(str2, "quic4@", 6) == 0) { |
| 1084 | str2 += 6; |
| 1085 | ss.ss_family = AF_INET; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1086 | proto_type = PROTO_TYPE_DGRAM; |
Frédéric Lécaille | 10caf65 | 2020-11-23 11:36:57 +0100 | [diff] [blame] | 1087 | ctrl_type = SOCK_STREAM; |
| 1088 | } |
| 1089 | else if (strncmp(str2, "quic6@", 6) == 0) { |
| 1090 | str2 += 6; |
| 1091 | ss.ss_family = AF_INET6; |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1092 | proto_type = PROTO_TYPE_DGRAM; |
Frédéric Lécaille | 10caf65 | 2020-11-23 11:36:57 +0100 | [diff] [blame] | 1093 | ctrl_type = SOCK_STREAM; |
| 1094 | } |
Willy Tarreau | 5a7beed | 2020-09-04 16:54:05 +0200 | [diff] [blame] | 1095 | else if (strncmp(str2, "fd@", 3) == 0) { |
| 1096 | str2 += 3; |
| 1097 | ss.ss_family = AF_CUST_EXISTING_FD; |
| 1098 | } |
| 1099 | else if (strncmp(str2, "sockpair@", 9) == 0) { |
| 1100 | str2 += 9; |
| 1101 | ss.ss_family = AF_CUST_SOCKPAIR; |
| 1102 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1103 | else if (*str2 == '/') { |
| 1104 | ss.ss_family = AF_UNIX; |
| 1105 | } |
| 1106 | else |
| 1107 | ss.ss_family = AF_UNSPEC; |
| 1108 | |
Willy Tarreau | 5a7beed | 2020-09-04 16:54:05 +0200 | [diff] [blame] | 1109 | if (ss.ss_family == AF_CUST_SOCKPAIR) { |
Willy Tarreau | a215be2 | 2020-09-16 10:14:16 +0200 | [diff] [blame] | 1110 | struct sockaddr_storage ss2; |
| 1111 | socklen_t addr_len; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [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) { |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 1116 | memprintf(err, "file descriptor '%s' is not a valid integer in '%s'\n", str2, str); |
| 1117 | goto out; |
| 1118 | } |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1119 | |
Willy Tarreau | a215be2 | 2020-09-16 10:14:16 +0200 | [diff] [blame] | 1120 | /* just verify that it's a socket */ |
| 1121 | addr_len = sizeof(ss2); |
| 1122 | if (getsockname(new_fd, (struct sockaddr *)&ss2, &addr_len) == -1) { |
| 1123 | memprintf(err, "cannot use file descriptor '%d' : %s.\n", new_fd, strerror(errno)); |
| 1124 | goto out; |
| 1125 | } |
| 1126 | |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1127 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = new_fd; |
| 1128 | ((struct sockaddr_in *)&ss)->sin_port = 0; |
William Lallemand | 2fe7dd0 | 2018-09-11 16:51:29 +0200 | [diff] [blame] | 1129 | } |
Willy Tarreau | 5a7beed | 2020-09-04 16:54:05 +0200 | [diff] [blame] | 1130 | else if (ss.ss_family == AF_CUST_EXISTING_FD) { |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 1131 | char *endptr; |
| 1132 | |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1133 | new_fd = strtol(str2, &endptr, 10); |
| 1134 | if (!*str2 || new_fd < 0 || *endptr) { |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 1135 | 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] | 1136 | goto out; |
| 1137 | } |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1138 | |
Willy Tarreau | 6edc722 | 2020-09-15 17:41:56 +0200 | [diff] [blame] | 1139 | if (opts & PA_O_SOCKET_FD) { |
| 1140 | socklen_t addr_len; |
| 1141 | int type; |
| 1142 | |
| 1143 | addr_len = sizeof(ss); |
| 1144 | if (getsockname(new_fd, (struct sockaddr *)&ss, &addr_len) == -1) { |
| 1145 | memprintf(err, "cannot use file descriptor '%d' : %s.\n", new_fd, strerror(errno)); |
| 1146 | goto out; |
| 1147 | } |
| 1148 | |
| 1149 | addr_len = sizeof(type); |
| 1150 | if (getsockopt(new_fd, SOL_SOCKET, SO_TYPE, &type, &addr_len) != 0 || |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1151 | (type == SOCK_STREAM) != (proto_type == PROTO_TYPE_STREAM)) { |
Willy Tarreau | 6edc722 | 2020-09-15 17:41:56 +0200 | [diff] [blame] | 1152 | memprintf(err, "socket on file descriptor '%d' is of the wrong type.\n", new_fd); |
| 1153 | goto out; |
| 1154 | } |
| 1155 | |
| 1156 | porta = portl = porth = get_host_port(&ss); |
| 1157 | } else if (opts & PA_O_RAW_FD) { |
| 1158 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = new_fd; |
| 1159 | ((struct sockaddr_in *)&ss)->sin_port = 0; |
| 1160 | } else { |
| 1161 | memprintf(err, "a file descriptor is not acceptable here in '%s'\n", str); |
| 1162 | goto out; |
| 1163 | } |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 1164 | } |
| 1165 | else if (ss.ss_family == AF_UNIX) { |
Willy Tarreau | 8daa920 | 2019-06-16 18:16:33 +0200 | [diff] [blame] | 1166 | struct sockaddr_un *un = (struct sockaddr_un *)&ss; |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1167 | int prefix_path_len; |
| 1168 | int max_path_len; |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 1169 | int adr_len; |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1170 | |
| 1171 | /* complete unix socket path name during startup or soft-restart is |
| 1172 | * <unix_bind_prefix><path>.<pid>.<bak|tmp> |
| 1173 | */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 1174 | prefix_path_len = (pfx && !abstract) ? strlen(pfx) : 0; |
Willy Tarreau | 8daa920 | 2019-06-16 18:16:33 +0200 | [diff] [blame] | 1175 | max_path_len = (sizeof(un->sun_path) - 1) - |
Willy Tarreau | 327ea5a | 2020-02-11 06:43:37 +0100 | [diff] [blame] | 1176 | (abstract ? 0 : prefix_path_len + 1 + 5 + 1 + 3); |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1177 | |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 1178 | adr_len = strlen(str2); |
| 1179 | if (adr_len > max_path_len) { |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 1180 | memprintf(err, "socket path '%s' too long (max %d)\n", str, max_path_len); |
| 1181 | goto out; |
| 1182 | } |
| 1183 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 1184 | /* 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] | 1185 | memset(un->sun_path, 0, sizeof(un->sun_path)); |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 1186 | if (prefix_path_len) |
Willy Tarreau | 8daa920 | 2019-06-16 18:16:33 +0200 | [diff] [blame] | 1187 | memcpy(un->sun_path, pfx, prefix_path_len); |
| 1188 | 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] | 1189 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1190 | else { /* IPv4 and IPv6 */ |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1191 | char *end = str2 + strlen(str2); |
| 1192 | char *chr; |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 1193 | |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1194 | /* search for : or ] whatever comes first */ |
| 1195 | for (chr = end-1; chr > str2; chr--) { |
| 1196 | if (*chr == ']' || *chr == ':') |
| 1197 | break; |
| 1198 | } |
| 1199 | |
| 1200 | if (*chr == ':') { |
| 1201 | /* Found a colon before a closing-bracket, must be a port separator. |
| 1202 | * This guarantee backward compatibility. |
| 1203 | */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1204 | if (!(opts & PA_O_PORT_OK)) { |
| 1205 | memprintf(err, "port specification not permitted here in '%s'", str); |
| 1206 | goto out; |
| 1207 | } |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1208 | *chr++ = '\0'; |
| 1209 | port1 = chr; |
| 1210 | } |
| 1211 | else { |
| 1212 | /* Either no colon and no closing-bracket |
| 1213 | * or directly ending with a closing-bracket. |
| 1214 | * However, no port. |
| 1215 | */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1216 | if (opts & PA_O_PORT_MAND) { |
| 1217 | memprintf(err, "missing port specification in '%s'", str); |
| 1218 | goto out; |
| 1219 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1220 | port1 = ""; |
mildis | ff5d510 | 2015-10-26 18:50:08 +0100 | [diff] [blame] | 1221 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1222 | |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1223 | if (isdigit((unsigned char)*port1)) { /* single port or range */ |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1224 | port2 = strchr(port1, '-'); |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1225 | if (port2) { |
| 1226 | if (!(opts & PA_O_PORT_RANGE)) { |
| 1227 | memprintf(err, "port range not permitted here in '%s'", str); |
| 1228 | goto out; |
| 1229 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1230 | *port2++ = '\0'; |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1231 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1232 | else |
| 1233 | port2 = port1; |
| 1234 | portl = atoi(port1); |
| 1235 | porth = atoi(port2); |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1236 | |
| 1237 | if (portl < !!(opts & PA_O_PORT_MAND) || portl > 65535) { |
| 1238 | memprintf(err, "invalid port '%s'", port1); |
| 1239 | goto out; |
| 1240 | } |
| 1241 | |
| 1242 | if (porth < !!(opts & PA_O_PORT_MAND) || porth > 65535) { |
| 1243 | memprintf(err, "invalid port '%s'", port2); |
| 1244 | goto out; |
| 1245 | } |
| 1246 | |
| 1247 | if (portl > porth) { |
| 1248 | memprintf(err, "invalid port range '%d-%d'", portl, porth); |
| 1249 | goto out; |
| 1250 | } |
| 1251 | |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1252 | porta = portl; |
| 1253 | } |
| 1254 | else if (*port1 == '-') { /* negative offset */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1255 | if (!(opts & PA_O_PORT_OFS)) { |
| 1256 | memprintf(err, "port offset not permitted here in '%s'", str); |
| 1257 | goto out; |
| 1258 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1259 | portl = atoi(port1 + 1); |
| 1260 | porta = -portl; |
| 1261 | } |
| 1262 | else if (*port1 == '+') { /* positive offset */ |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1263 | if (!(opts & PA_O_PORT_OFS)) { |
| 1264 | memprintf(err, "port offset not permitted here in '%s'", str); |
| 1265 | goto out; |
| 1266 | } |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1267 | porth = atoi(port1 + 1); |
| 1268 | porta = porth; |
| 1269 | } |
| 1270 | else if (*port1) { /* other any unexpected char */ |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 1271 | 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] | 1272 | goto out; |
| 1273 | } |
Willy Tarreau | 7f96a84 | 2020-09-15 11:12:44 +0200 | [diff] [blame] | 1274 | else if (opts & PA_O_PORT_MAND) { |
| 1275 | memprintf(err, "missing port specification in '%s'", str); |
| 1276 | goto out; |
| 1277 | } |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1278 | |
| 1279 | /* first try to parse the IP without resolving. If it fails, it |
| 1280 | * tells us we need to keep a copy of the FQDN to resolve later |
| 1281 | * 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] | 1282 | * 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] | 1283 | */ |
| 1284 | if (str2ip2(str2, &ss, 0) == NULL) { |
Willy Tarreau | cd3a5591 | 2020-09-04 15:30:46 +0200 | [diff] [blame] | 1285 | if ((!(opts & PA_O_RESOLVE) && !fqdn) || |
| 1286 | ((opts & PA_O_RESOLVE) && str2ip2(str2, &ss, 1) == NULL)) { |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1287 | memprintf(err, "invalid address: '%s' in '%s'\n", str2, str); |
| 1288 | goto out; |
| 1289 | } |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 1290 | |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1291 | if (fqdn) { |
| 1292 | if (str2 != back) |
| 1293 | memmove(back, str2, strlen(str2) + 1); |
| 1294 | *fqdn = back; |
| 1295 | back = NULL; |
| 1296 | } |
Willy Tarreau | 72b8c1f | 2015-09-08 15:50:19 +0200 | [diff] [blame] | 1297 | } |
Willy Tarreau | ceccdd7 | 2016-11-02 22:27:10 +0100 | [diff] [blame] | 1298 | set_host_port(&ss, porta); |
Willy Tarreau | e4c58c8 | 2013-03-06 15:28:17 +0100 | [diff] [blame] | 1299 | } |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 1300 | |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1301 | if (ctrl_type == SOCK_STREAM && !(opts & PA_O_STREAM)) { |
Willy Tarreau | 8d31ab0 | 2022-05-09 16:18:26 +0200 | [diff] [blame] | 1302 | memprintf(err, "stream-type address not acceptable in '%s'\n", str); |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1303 | goto out; |
| 1304 | } |
| 1305 | else if (ctrl_type == SOCK_DGRAM && !(opts & PA_O_DGRAM)) { |
Willy Tarreau | 8d31ab0 | 2022-05-09 16:18:26 +0200 | [diff] [blame] | 1306 | memprintf(err, "dgram-type address not acceptable in '%s'\n", str); |
Willy Tarreau | e835bd8 | 2020-09-16 11:35:47 +0200 | [diff] [blame] | 1307 | goto out; |
| 1308 | } |
| 1309 | |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1310 | if (proto || (opts & PA_O_CONNECT)) { |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1311 | /* Note: if the caller asks for a proto, we must find one, |
Emeric Brun | 2675490 | 2021-04-07 14:26:44 +0200 | [diff] [blame] | 1312 | * except if we inherit from a raw FD (family == AF_CUST_EXISTING_FD) |
| 1313 | * orif we return with an fqdn that will resolve later, |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1314 | * in which case the address is not known yet (this is only |
| 1315 | * for servers actually). |
| 1316 | */ |
Willy Tarreau | b2ffc99 | 2020-09-16 21:37:31 +0200 | [diff] [blame] | 1317 | new_proto = protocol_lookup(ss.ss_family, |
Willy Tarreau | e3b4518 | 2021-10-27 17:28:55 +0200 | [diff] [blame] | 1318 | proto_type, |
Willy Tarreau | af9609b | 2020-09-16 22:04:46 +0200 | [diff] [blame] | 1319 | ctrl_type == SOCK_DGRAM); |
Willy Tarreau | b2ffc99 | 2020-09-16 21:37:31 +0200 | [diff] [blame] | 1320 | |
Emeric Brun | 2675490 | 2021-04-07 14:26:44 +0200 | [diff] [blame] | 1321 | if (!new_proto && (!fqdn || !*fqdn) && (ss.ss_family != AF_CUST_EXISTING_FD)) { |
Willy Tarreau | 3d7b468 | 2022-05-20 17:32:35 +0200 | [diff] [blame] | 1322 | memprintf(err, "unsupported %s protocol for %s family %d address '%s'%s", |
Willy Tarreau | 2b049b8 | 2022-05-20 17:28:30 +0200 | [diff] [blame] | 1323 | (ctrl_type == SOCK_DGRAM) ? "datagram" : "stream", |
| 1324 | (proto_type == PROTO_TYPE_DGRAM) ? "datagram" : "stream", |
| 1325 | ss.ss_family, |
Willy Tarreau | 3d7b468 | 2022-05-20 17:32:35 +0200 | [diff] [blame] | 1326 | str, |
Willy Tarreau | 3d7b468 | 2022-05-20 17:32:35 +0200 | [diff] [blame] | 1327 | #ifndef USE_QUIC |
Tim Duesterhus | 147eeb2 | 2022-05-22 12:40:58 +0200 | [diff] [blame] | 1328 | (ctrl_type == SOCK_STREAM && proto_type == PROTO_TYPE_DGRAM) |
| 1329 | ? "; QUIC is not compiled in if this is what you were looking for." |
| 1330 | : "" |
Willy Tarreau | 3d7b468 | 2022-05-20 17:32:35 +0200 | [diff] [blame] | 1331 | #else |
| 1332 | "" |
| 1333 | #endif |
Tim Duesterhus | 147eeb2 | 2022-05-22 12:40:58 +0200 | [diff] [blame] | 1334 | ); |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1335 | goto out; |
| 1336 | } |
Willy Tarreau | 65ec4e3 | 2020-09-16 19:17:08 +0200 | [diff] [blame] | 1337 | |
| 1338 | if ((opts & PA_O_CONNECT) && new_proto && !new_proto->connect) { |
| 1339 | memprintf(err, "connect() not supported for this protocol family %d used by address '%s'", ss.ss_family, str); |
| 1340 | goto out; |
| 1341 | } |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1342 | } |
| 1343 | |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 1344 | ret = &ss; |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 1345 | out: |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 1346 | if (port) |
| 1347 | *port = porta; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 1348 | if (low) |
| 1349 | *low = portl; |
| 1350 | if (high) |
| 1351 | *high = porth; |
Willy Tarreau | a93e5c7 | 2020-09-15 14:01:16 +0200 | [diff] [blame] | 1352 | if (fd) |
| 1353 | *fd = new_fd; |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 1354 | if (proto) |
| 1355 | *proto = new_proto; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 1356 | free(back); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 1357 | return ret; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1358 | } |
| 1359 | |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 1360 | /* converts <addr> and <port> into a string representation of the address and port. This is sort |
| 1361 | * of an inverse of str2sa_range, with some restrictions. The supported families are AF_INET, |
| 1362 | * AF_INET6, AF_UNIX, and AF_CUST_SOCKPAIR. If the family is unsopported NULL is returned. |
| 1363 | * If map_ports is true, then the sign of the port is included in the output, to indicate it is |
| 1364 | * relative to the incoming port. AF_INET and AF_INET6 will be in the form "<addr>:<port>". |
| 1365 | * AF_UNIX will either be just the path (if using a pathname) or "abns@<path>" if it is abstract. |
| 1366 | * AF_CUST_SOCKPAIR will be of the form "sockpair@<fd>". |
| 1367 | * |
| 1368 | * The returned char* is allocated, and it is the responsibility of the caller to free it. |
| 1369 | */ |
| 1370 | char * sa2str(const struct sockaddr_storage *addr, int port, int map_ports) |
| 1371 | { |
| 1372 | char buffer[INET6_ADDRSTRLEN]; |
| 1373 | char *out = NULL; |
| 1374 | const void *ptr; |
| 1375 | const char *path; |
| 1376 | |
| 1377 | switch (addr->ss_family) { |
| 1378 | case AF_INET: |
| 1379 | ptr = &((struct sockaddr_in *)addr)->sin_addr; |
| 1380 | break; |
| 1381 | case AF_INET6: |
| 1382 | ptr = &((struct sockaddr_in6 *)addr)->sin6_addr; |
| 1383 | break; |
| 1384 | case AF_UNIX: |
| 1385 | path = ((struct sockaddr_un *)addr)->sun_path; |
| 1386 | if (path[0] == '\0') { |
| 1387 | const int max_length = sizeof(struct sockaddr_un) - offsetof(struct sockaddr_un, sun_path) - 1; |
| 1388 | return memprintf(&out, "abns@%.*s", max_length, path+1); |
| 1389 | } else { |
| 1390 | return strdup(path); |
| 1391 | } |
| 1392 | case AF_CUST_SOCKPAIR: |
| 1393 | return memprintf(&out, "sockpair@%d", ((struct sockaddr_in *)addr)->sin_addr.s_addr); |
| 1394 | default: |
| 1395 | return NULL; |
| 1396 | } |
Tim Duesterhus | 22535a5 | 2022-05-23 09:30:49 +0200 | [diff] [blame] | 1397 | if (inet_ntop(addr->ss_family, ptr, buffer, sizeof(buffer)) == NULL) { |
| 1398 | BUG_ON(errno == ENOSPC); |
| 1399 | return NULL; |
| 1400 | } |
Thayne McCombs | 92149f9 | 2020-11-20 01:28:26 -0700 | [diff] [blame] | 1401 | if (map_ports) |
| 1402 | return memprintf(&out, "%s:%+d", buffer, port); |
| 1403 | else |
| 1404 | return memprintf(&out, "%s:%d", buffer, port); |
| 1405 | } |
| 1406 | |
| 1407 | |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1408 | /* converts <str> to a struct in_addr containing a network mask. It can be |
| 1409 | * 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] | 1410 | * if the conversion succeeds otherwise zero. |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1411 | */ |
| 1412 | int str2mask(const char *str, struct in_addr *mask) |
| 1413 | { |
| 1414 | if (strchr(str, '.') != NULL) { /* dotted notation */ |
| 1415 | if (!inet_pton(AF_INET, str, mask)) |
| 1416 | return 0; |
| 1417 | } |
| 1418 | else { /* mask length */ |
| 1419 | char *err; |
| 1420 | unsigned long len = strtol(str, &err, 10); |
| 1421 | |
| 1422 | if (!*str || (err && *err) || (unsigned)len > 32) |
| 1423 | return 0; |
Tim Duesterhus | 8575f72 | 2018-01-25 16:24:48 +0100 | [diff] [blame] | 1424 | |
| 1425 | len2mask4(len, mask); |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1426 | } |
| 1427 | return 1; |
| 1428 | } |
| 1429 | |
Tim Duesterhus | 4718517 | 2018-01-25 16:24:49 +0100 | [diff] [blame] | 1430 | /* 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] | 1431 | * 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] | 1432 | * if the conversion succeeds otherwise zero. |
| 1433 | */ |
| 1434 | int str2mask6(const char *str, struct in6_addr *mask) |
| 1435 | { |
| 1436 | if (strchr(str, ':') != NULL) { /* quadruplet notation */ |
| 1437 | if (!inet_pton(AF_INET6, str, mask)) |
| 1438 | return 0; |
| 1439 | } |
| 1440 | else { /* mask length */ |
| 1441 | char *err; |
| 1442 | unsigned long len = strtol(str, &err, 10); |
| 1443 | |
| 1444 | if (!*str || (err && *err) || (unsigned)len > 128) |
| 1445 | return 0; |
| 1446 | |
| 1447 | len2mask6(len, mask); |
| 1448 | } |
| 1449 | return 1; |
| 1450 | } |
| 1451 | |
Thierry FOURNIER | b050463 | 2013-12-14 15:39:02 +0100 | [diff] [blame] | 1452 | /* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion |
| 1453 | * succeeds otherwise zero. |
| 1454 | */ |
| 1455 | int cidr2dotted(int cidr, struct in_addr *mask) { |
| 1456 | |
| 1457 | if (cidr < 0 || cidr > 32) |
| 1458 | return 0; |
| 1459 | |
| 1460 | mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0; |
| 1461 | return 1; |
| 1462 | } |
| 1463 | |
Thierry Fournier | 70473a5 | 2016-02-17 17:12:14 +0100 | [diff] [blame] | 1464 | /* Convert mask from bit length form to in_addr form. |
| 1465 | * This function never fails. |
| 1466 | */ |
| 1467 | void len2mask4(int len, struct in_addr *addr) |
| 1468 | { |
| 1469 | if (len >= 32) { |
| 1470 | addr->s_addr = 0xffffffff; |
| 1471 | return; |
| 1472 | } |
| 1473 | if (len <= 0) { |
| 1474 | addr->s_addr = 0x00000000; |
| 1475 | return; |
| 1476 | } |
| 1477 | addr->s_addr = 0xffffffff << (32 - len); |
| 1478 | addr->s_addr = htonl(addr->s_addr); |
| 1479 | } |
| 1480 | |
| 1481 | /* Convert mask from bit length form to in6_addr form. |
| 1482 | * This function never fails. |
| 1483 | */ |
| 1484 | void len2mask6(int len, struct in6_addr *addr) |
| 1485 | { |
| 1486 | len2mask4(len, (struct in_addr *)&addr->s6_addr[0]); /* msb */ |
| 1487 | len -= 32; |
| 1488 | len2mask4(len, (struct in_addr *)&addr->s6_addr[4]); |
| 1489 | len -= 32; |
| 1490 | len2mask4(len, (struct in_addr *)&addr->s6_addr[8]); |
| 1491 | len -= 32; |
| 1492 | len2mask4(len, (struct in_addr *)&addr->s6_addr[12]); /* lsb */ |
| 1493 | } |
| 1494 | |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 1495 | /* |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 1496 | * converts <str> to two struct in_addr* which must be pre-allocated. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1497 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1498 | * is optional and either in the dotted or CIDR notation. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1499 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 1500 | */ |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 1501 | 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] | 1502 | { |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1503 | __label__ out_free, out_err; |
| 1504 | char *c, *s; |
| 1505 | int ret_val; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1506 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1507 | s = strdup(str); |
| 1508 | if (!s) |
| 1509 | return 0; |
| 1510 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1511 | memset(mask, 0, sizeof(*mask)); |
| 1512 | memset(addr, 0, sizeof(*addr)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1513 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1514 | if ((c = strrchr(s, '/')) != NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1515 | *c++ = '\0'; |
| 1516 | /* c points to the mask */ |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 1517 | if (!str2mask(c, mask)) |
| 1518 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1519 | } |
| 1520 | else { |
Willy Tarreau | ebd6160 | 2006-12-30 11:54:15 +0100 | [diff] [blame] | 1521 | mask->s_addr = ~0U; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1522 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1523 | if (!inet_pton(AF_INET, s, addr)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1524 | struct hostent *he; |
| 1525 | |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 1526 | if (!resolve) |
| 1527 | goto out_err; |
| 1528 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1529 | if ((he = gethostbyname(s)) == NULL) { |
| 1530 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1531 | } |
| 1532 | else |
| 1533 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 1534 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 1535 | |
| 1536 | ret_val = 1; |
| 1537 | out_free: |
| 1538 | free(s); |
| 1539 | return ret_val; |
| 1540 | out_err: |
| 1541 | ret_val = 0; |
| 1542 | goto out_free; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1543 | } |
| 1544 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1545 | |
| 1546 | /* |
Willy Tarreau | 6d20e28 | 2012-04-27 22:49:47 +0200 | [diff] [blame] | 1547 | * converts <str> to two struct in6_addr* which must be pre-allocated. |
| 1548 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1549 | * is an optional number of bits (128 being the default). |
Willy Tarreau | 6d20e28 | 2012-04-27 22:49:47 +0200 | [diff] [blame] | 1550 | * Returns 1 if OK, 0 if error. |
| 1551 | */ |
| 1552 | int str62net(const char *str, struct in6_addr *addr, unsigned char *mask) |
| 1553 | { |
| 1554 | char *c, *s; |
| 1555 | int ret_val = 0; |
| 1556 | char *err; |
| 1557 | unsigned long len = 128; |
| 1558 | |
| 1559 | s = strdup(str); |
| 1560 | if (!s) |
| 1561 | return 0; |
| 1562 | |
| 1563 | memset(mask, 0, sizeof(*mask)); |
| 1564 | memset(addr, 0, sizeof(*addr)); |
| 1565 | |
| 1566 | if ((c = strrchr(s, '/')) != NULL) { |
| 1567 | *c++ = '\0'; /* c points to the mask */ |
| 1568 | if (!*c) |
| 1569 | goto out_free; |
| 1570 | |
| 1571 | len = strtoul(c, &err, 10); |
| 1572 | if ((err && *err) || (unsigned)len > 128) |
| 1573 | goto out_free; |
| 1574 | } |
| 1575 | *mask = len; /* OK we have a valid mask in <len> */ |
| 1576 | |
| 1577 | if (!inet_pton(AF_INET6, s, addr)) |
| 1578 | goto out_free; |
| 1579 | |
| 1580 | ret_val = 1; |
| 1581 | out_free: |
| 1582 | free(s); |
| 1583 | return ret_val; |
| 1584 | } |
| 1585 | |
| 1586 | |
| 1587 | /* |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1588 | * Parse IPv4 address found in url. Return the number of bytes parsed. It |
| 1589 | * expects exactly 4 numbers between 0 and 255 delimited by dots, and returns |
| 1590 | * zero in case of mismatch. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1591 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 1592 | int url2ipv4(const char *addr, struct in_addr *dst) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1593 | { |
| 1594 | int saw_digit, octets, ch; |
| 1595 | u_char tmp[4], *tp; |
| 1596 | const char *cp = addr; |
| 1597 | |
| 1598 | saw_digit = 0; |
| 1599 | octets = 0; |
| 1600 | *(tp = tmp) = 0; |
| 1601 | |
| 1602 | while (*addr) { |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1603 | unsigned char digit = (ch = *addr) - '0'; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1604 | if (digit > 9 && ch != '.') |
| 1605 | break; |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1606 | addr++; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1607 | if (digit <= 9) { |
| 1608 | u_int new = *tp * 10 + digit; |
| 1609 | if (new > 255) |
| 1610 | return 0; |
| 1611 | *tp = new; |
| 1612 | if (!saw_digit) { |
| 1613 | if (++octets > 4) |
| 1614 | return 0; |
| 1615 | saw_digit = 1; |
| 1616 | } |
| 1617 | } else if (ch == '.' && saw_digit) { |
| 1618 | if (octets == 4) |
| 1619 | return 0; |
| 1620 | *++tp = 0; |
| 1621 | saw_digit = 0; |
| 1622 | } else |
| 1623 | return 0; |
| 1624 | } |
| 1625 | |
| 1626 | if (octets < 4) |
| 1627 | return 0; |
| 1628 | |
| 1629 | memcpy(&dst->s_addr, tmp, 4); |
Willy Tarreau | 12e1027 | 2021-03-25 11:34:40 +0100 | [diff] [blame] | 1630 | return addr - cp; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1631 | } |
| 1632 | |
| 1633 | /* |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1634 | * Resolve destination server from URL. Convert <str> to a sockaddr_storage. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1635 | * <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] | 1636 | * the hostname. Actually only http and https are supported. <out> can be NULL. |
| 1637 | * This function returns the consumed length. It is useful if you parse complete |
| 1638 | * url like http://host:port/path, because the consumed length corresponds to |
| 1639 | * the first character of the path. If the conversion fails, it returns -1. |
| 1640 | * |
| 1641 | * This function tries to resolve the DNS name if haproxy is in starting mode. |
| 1642 | * So, this function may be used during the configuration parsing. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1643 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1644 | 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] | 1645 | { |
| 1646 | const char *curr = url, *cp = url; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1647 | const char *end; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1648 | int ret, url_code = 0; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1649 | unsigned long long int http_code = 0; |
| 1650 | int default_port; |
| 1651 | struct hostent *he; |
| 1652 | char *p; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1653 | |
| 1654 | /* Firstly, try to find :// pattern */ |
| 1655 | while (curr < url+ulen && url_code != 0x3a2f2f) { |
| 1656 | url_code = ((url_code & 0xffff) << 8); |
| 1657 | url_code += (unsigned char)*curr++; |
| 1658 | } |
| 1659 | |
| 1660 | /* Secondly, if :// pattern is found, verify parsed stuff |
| 1661 | * before pattern is matching our http pattern. |
| 1662 | * If so parse ip address and port in uri. |
| 1663 | * |
| 1664 | * WARNING: Current code doesn't support dynamic async dns resolver. |
| 1665 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1666 | if (url_code != 0x3a2f2f) |
| 1667 | return -1; |
| 1668 | |
| 1669 | /* Copy scheme, and utrn to lower case. */ |
| 1670 | while (cp < curr - 3) |
| 1671 | http_code = (http_code << 8) + *cp++; |
| 1672 | http_code |= 0x2020202020202020ULL; /* Turn everything to lower case */ |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1673 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1674 | /* HTTP or HTTPS url matching */ |
| 1675 | if (http_code == 0x2020202068747470ULL) { |
| 1676 | default_port = 80; |
| 1677 | if (out) |
| 1678 | out->scheme = SCH_HTTP; |
| 1679 | } |
| 1680 | else if (http_code == 0x2020206874747073ULL) { |
| 1681 | default_port = 443; |
| 1682 | if (out) |
| 1683 | out->scheme = SCH_HTTPS; |
| 1684 | } |
| 1685 | else |
| 1686 | return -1; |
| 1687 | |
| 1688 | /* If the next char is '[', the host address is IPv6. */ |
| 1689 | if (*curr == '[') { |
| 1690 | curr++; |
| 1691 | |
| 1692 | /* Check trash size */ |
| 1693 | if (trash.size < ulen) |
| 1694 | return -1; |
| 1695 | |
| 1696 | /* Look for ']' and copy the address in a trash buffer. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1697 | p = trash.area; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1698 | for (end = curr; |
| 1699 | end < url + ulen && *end != ']'; |
| 1700 | end++, p++) |
| 1701 | *p = *end; |
| 1702 | if (*end != ']') |
| 1703 | return -1; |
| 1704 | *p = '\0'; |
| 1705 | |
| 1706 | /* Update out. */ |
| 1707 | if (out) { |
| 1708 | out->host = curr; |
| 1709 | out->host_len = end - curr; |
| 1710 | } |
| 1711 | |
| 1712 | /* Try IPv6 decoding. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1713 | 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] | 1714 | return -1; |
| 1715 | end++; |
| 1716 | |
| 1717 | /* Decode port. */ |
William Lallemand | 3d7a918 | 2022-03-25 17:37:51 +0100 | [diff] [blame] | 1718 | if (end < url + ulen && *end == ':') { |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1719 | end++; |
| 1720 | default_port = read_uint(&end, url + ulen); |
| 1721 | } |
| 1722 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port); |
| 1723 | ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; |
| 1724 | return end - url; |
| 1725 | } |
| 1726 | else { |
William Lallemand | 8a91374 | 2022-02-18 16:13:12 +0100 | [diff] [blame] | 1727 | /* we need to copy the string into the trash because url2ipv4 |
| 1728 | * needs a \0 at the end of the string */ |
| 1729 | if (trash.size < ulen) |
| 1730 | return -1; |
| 1731 | |
| 1732 | memcpy(trash.area, curr, ulen - (curr - url)); |
| 1733 | trash.area[ulen - (curr - url)] = '\0'; |
| 1734 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1735 | /* We are looking for IP address. If you want to parse and |
| 1736 | * resolve hostname found in url, you can use str2sa_range(), but |
| 1737 | * be warned this can slow down global daemon performances |
| 1738 | * while handling lagging dns responses. |
| 1739 | */ |
William Lallemand | 8a91374 | 2022-02-18 16:13:12 +0100 | [diff] [blame] | 1740 | ret = url2ipv4(trash.area, &((struct sockaddr_in *)addr)->sin_addr); |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1741 | if (ret) { |
| 1742 | /* Update out. */ |
| 1743 | if (out) { |
| 1744 | out->host = curr; |
| 1745 | out->host_len = ret; |
| 1746 | } |
| 1747 | |
William Lallemand | b938b77 | 2022-03-24 21:59:03 +0100 | [diff] [blame] | 1748 | curr += ret; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1749 | |
| 1750 | /* Decode port. */ |
William Lallemand | 3d7a918 | 2022-03-25 17:37:51 +0100 | [diff] [blame] | 1751 | if (curr < url + ulen && *curr == ':') { |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1752 | curr++; |
| 1753 | default_port = read_uint(&curr, url + ulen); |
| 1754 | } |
| 1755 | ((struct sockaddr_in *)addr)->sin_port = htons(default_port); |
| 1756 | |
| 1757 | /* Set family. */ |
| 1758 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 1759 | return curr - url; |
| 1760 | } |
| 1761 | else if (global.mode & MODE_STARTING) { |
| 1762 | /* The IPv4 and IPv6 decoding fails, maybe the url contain name. Try to execute |
| 1763 | * synchronous DNS request only if HAProxy is in the start state. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1764 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1765 | |
| 1766 | /* look for : or / or end */ |
| 1767 | for (end = curr; |
| 1768 | end < url + ulen && *end != '/' && *end != ':'; |
| 1769 | end++); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1770 | memcpy(trash.area, curr, end - curr); |
| 1771 | trash.area[end - curr] = '\0'; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1772 | |
| 1773 | /* try to resolve an IPv4/IPv6 hostname */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1774 | he = gethostbyname(trash.area); |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1775 | if (!he) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1776 | return -1; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1777 | |
| 1778 | /* Update out. */ |
| 1779 | if (out) { |
| 1780 | out->host = curr; |
| 1781 | out->host_len = end - curr; |
| 1782 | } |
| 1783 | |
| 1784 | /* Decode port. */ |
William Lallemand | 3d7a918 | 2022-03-25 17:37:51 +0100 | [diff] [blame] | 1785 | if (end < url + ulen && *end == ':') { |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1786 | end++; |
| 1787 | default_port = read_uint(&end, url + ulen); |
| 1788 | } |
| 1789 | |
| 1790 | /* Copy IP address, set port and family. */ |
| 1791 | switch (he->h_addrtype) { |
| 1792 | case AF_INET: |
| 1793 | ((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 1794 | ((struct sockaddr_in *)addr)->sin_port = htons(default_port); |
| 1795 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 1796 | return end - url; |
| 1797 | |
| 1798 | case AF_INET6: |
| 1799 | ((struct sockaddr_in6 *)addr)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list); |
| 1800 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port); |
| 1801 | ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; |
| 1802 | return end - url; |
| 1803 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1804 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1805 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1806 | return -1; |
| 1807 | } |
| 1808 | |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1809 | /* Tries to convert a sockaddr_storage address to text form. Upon success, the |
| 1810 | * address family is returned so that it's easy for the caller to adapt to the |
| 1811 | * output format. Zero is returned if the address family is not supported. -1 |
| 1812 | * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are |
| 1813 | * supported. |
| 1814 | */ |
Willy Tarreau | d5ec4bf | 2019-04-25 17:48:16 +0200 | [diff] [blame] | 1815 | 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] | 1816 | { |
| 1817 | |
Willy Tarreau | d5ec4bf | 2019-04-25 17:48:16 +0200 | [diff] [blame] | 1818 | const void *ptr; |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1819 | |
| 1820 | if (size < 5) |
| 1821 | return 0; |
| 1822 | *str = '\0'; |
| 1823 | |
| 1824 | switch (addr->ss_family) { |
| 1825 | case AF_INET: |
| 1826 | ptr = &((struct sockaddr_in *)addr)->sin_addr; |
| 1827 | break; |
| 1828 | case AF_INET6: |
| 1829 | ptr = &((struct sockaddr_in6 *)addr)->sin6_addr; |
| 1830 | break; |
| 1831 | case AF_UNIX: |
| 1832 | memcpy(str, "unix", 5); |
| 1833 | return addr->ss_family; |
| 1834 | default: |
| 1835 | return 0; |
| 1836 | } |
| 1837 | |
| 1838 | if (inet_ntop(addr->ss_family, ptr, str, size)) |
| 1839 | return addr->ss_family; |
| 1840 | |
| 1841 | /* failed */ |
| 1842 | return -1; |
| 1843 | } |
| 1844 | |
Simon Horman | 75ab8bd | 2014-06-16 09:39:41 +0900 | [diff] [blame] | 1845 | /* Tries to convert a sockaddr_storage port to text form. Upon success, the |
| 1846 | * address family is returned so that it's easy for the caller to adapt to the |
| 1847 | * output format. Zero is returned if the address family is not supported. -1 |
| 1848 | * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are |
| 1849 | * supported. |
| 1850 | */ |
Willy Tarreau | d5ec4bf | 2019-04-25 17:48:16 +0200 | [diff] [blame] | 1851 | 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] | 1852 | { |
| 1853 | |
| 1854 | uint16_t port; |
| 1855 | |
| 1856 | |
Willy Tarreau | d7dad1b | 2017-01-06 16:46:22 +0100 | [diff] [blame] | 1857 | if (size < 6) |
Simon Horman | 75ab8bd | 2014-06-16 09:39:41 +0900 | [diff] [blame] | 1858 | return 0; |
| 1859 | *str = '\0'; |
| 1860 | |
| 1861 | switch (addr->ss_family) { |
| 1862 | case AF_INET: |
| 1863 | port = ((struct sockaddr_in *)addr)->sin_port; |
| 1864 | break; |
| 1865 | case AF_INET6: |
| 1866 | port = ((struct sockaddr_in6 *)addr)->sin6_port; |
| 1867 | break; |
| 1868 | case AF_UNIX: |
| 1869 | memcpy(str, "unix", 5); |
| 1870 | return addr->ss_family; |
| 1871 | default: |
| 1872 | return 0; |
| 1873 | } |
| 1874 | |
| 1875 | snprintf(str, size, "%u", ntohs(port)); |
| 1876 | return addr->ss_family; |
| 1877 | } |
| 1878 | |
Willy Tarreau | 16e0156 | 2016-08-09 16:46:18 +0200 | [diff] [blame] | 1879 | /* check if the given address is local to the system or not. It will return |
| 1880 | * -1 when it's not possible to know, 0 when the address is not local, 1 when |
| 1881 | * it is. We don't want to iterate over all interfaces for this (and it is not |
| 1882 | * portable). So instead we try to bind in UDP to this address on a free non |
| 1883 | * privileged port and to connect to the same address, port 0 (connect doesn't |
| 1884 | * care). If it succeeds, we own the address. Note that non-inet addresses are |
| 1885 | * considered local since they're most likely AF_UNIX. |
| 1886 | */ |
| 1887 | int addr_is_local(const struct netns_entry *ns, |
| 1888 | const struct sockaddr_storage *orig) |
| 1889 | { |
| 1890 | struct sockaddr_storage addr; |
| 1891 | int result; |
| 1892 | int fd; |
| 1893 | |
| 1894 | if (!is_inet_addr(orig)) |
| 1895 | return 1; |
| 1896 | |
| 1897 | memcpy(&addr, orig, sizeof(addr)); |
| 1898 | set_host_port(&addr, 0); |
| 1899 | |
| 1900 | fd = my_socketat(ns, addr.ss_family, SOCK_DGRAM, IPPROTO_UDP); |
| 1901 | if (fd < 0) |
| 1902 | return -1; |
| 1903 | |
| 1904 | result = -1; |
| 1905 | if (bind(fd, (struct sockaddr *)&addr, get_addr_len(&addr)) == 0) { |
| 1906 | if (connect(fd, (struct sockaddr *)&addr, get_addr_len(&addr)) == -1) |
| 1907 | result = 0; // fail, non-local address |
| 1908 | else |
| 1909 | result = 1; // success, local address |
| 1910 | } |
| 1911 | else { |
| 1912 | if (errno == EADDRNOTAVAIL) |
| 1913 | result = 0; // definitely not local :-) |
| 1914 | } |
| 1915 | close(fd); |
| 1916 | |
| 1917 | return result; |
| 1918 | } |
| 1919 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1920 | /* will try to encode the string <string> replacing all characters tagged in |
| 1921 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 1922 | * prefixed by <escape>, and will store the result between <start> (included) |
| 1923 | * and <stop> (excluded), and will always terminate the string with a '\0' |
| 1924 | * before <stop>. The position of the '\0' is returned if the conversion |
| 1925 | * completes. If bytes are missing between <start> and <stop>, then the |
| 1926 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 1927 | * cannot even be stored so we return <start> without writing the 0. |
| 1928 | * The input string must also be zero-terminated. |
| 1929 | */ |
| 1930 | const char hextab[16] = "0123456789ABCDEF"; |
| 1931 | char *encode_string(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1932 | const char escape, const long *map, |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1933 | const char *string) |
| 1934 | { |
| 1935 | if (start < stop) { |
| 1936 | stop--; /* reserve one byte for the final '\0' */ |
| 1937 | while (start < stop && *string != '\0') { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1938 | if (!ha_bit_test((unsigned char)(*string), map)) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1939 | *start++ = *string; |
| 1940 | else { |
| 1941 | if (start + 3 >= stop) |
| 1942 | break; |
| 1943 | *start++ = escape; |
| 1944 | *start++ = hextab[(*string >> 4) & 15]; |
| 1945 | *start++ = hextab[*string & 15]; |
| 1946 | } |
| 1947 | string++; |
| 1948 | } |
| 1949 | *start = '\0'; |
| 1950 | } |
| 1951 | return start; |
| 1952 | } |
| 1953 | |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1954 | /* |
| 1955 | * Same behavior as encode_string() above, except that it encodes chunk |
| 1956 | * <chunk> instead of a string. |
| 1957 | */ |
| 1958 | char *encode_chunk(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1959 | const char escape, const long *map, |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 1960 | const struct buffer *chunk) |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1961 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1962 | char *str = chunk->area; |
| 1963 | char *end = chunk->area + chunk->data; |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1964 | |
| 1965 | if (start < stop) { |
| 1966 | stop--; /* reserve one byte for the final '\0' */ |
| 1967 | while (start < stop && str < end) { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1968 | if (!ha_bit_test((unsigned char)(*str), map)) |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1969 | *start++ = *str; |
| 1970 | else { |
| 1971 | if (start + 3 >= stop) |
| 1972 | break; |
| 1973 | *start++ = escape; |
| 1974 | *start++ = hextab[(*str >> 4) & 15]; |
| 1975 | *start++ = hextab[*str & 15]; |
| 1976 | } |
| 1977 | str++; |
| 1978 | } |
| 1979 | *start = '\0'; |
| 1980 | } |
| 1981 | return start; |
| 1982 | } |
| 1983 | |
Dragan Dosen | 0edd109 | 2016-02-12 13:23:02 +0100 | [diff] [blame] | 1984 | /* |
| 1985 | * Tries to prefix characters tagged in the <map> with the <escape> |
Aurelien DARRAGON | c5bff8e | 2022-09-20 14:33:18 +0200 | [diff] [blame] | 1986 | * character. The input <string> is processed until string_stop |
| 1987 | * is reached or NULL-byte is encountered. The result will |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 1988 | * be stored between <start> (included) and <stop> (excluded). This |
| 1989 | * function will always try to terminate the resulting string with a '\0' |
| 1990 | * before <stop>, and will return its position if the conversion |
| 1991 | * completes. |
| 1992 | */ |
| 1993 | char *escape_string(char *start, char *stop, |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 1994 | const char escape, const long *map, |
Aurelien DARRAGON | c5bff8e | 2022-09-20 14:33:18 +0200 | [diff] [blame] | 1995 | const char *string, const char *string_stop) |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 1996 | { |
| 1997 | if (start < stop) { |
| 1998 | stop--; /* reserve one byte for the final '\0' */ |
Aurelien DARRAGON | c5bff8e | 2022-09-20 14:33:18 +0200 | [diff] [blame] | 1999 | while (start < stop && string < string_stop && *string != '\0') { |
Willy Tarreau | 1bfd602 | 2019-06-07 11:10:07 +0200 | [diff] [blame] | 2000 | if (!ha_bit_test((unsigned char)(*string), map)) |
Dragan Dosen | 1a5d060 | 2016-07-22 16:00:31 +0200 | [diff] [blame] | 2001 | *start++ = *string; |
| 2002 | else { |
| 2003 | if (start + 2 >= stop) |
| 2004 | break; |
| 2005 | *start++ = escape; |
| 2006 | *start++ = *string; |
| 2007 | } |
| 2008 | string++; |
| 2009 | } |
| 2010 | *start = '\0'; |
| 2011 | } |
| 2012 | return start; |
| 2013 | } |
| 2014 | |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2015 | /* Check a string for using it in a CSV output format. If the string contains |
| 2016 | * one of the following four char <">, <,>, CR or LF, the string is |
| 2017 | * encapsulated between <"> and the <"> are escaped by a <""> sequence. |
| 2018 | * <str> is the input string to be escaped. The function assumes that |
| 2019 | * the input string is null-terminated. |
| 2020 | * |
| 2021 | * 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] | 2022 | * 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] | 2023 | * format. |
| 2024 | * |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2025 | * printf("..., \"%s\", ...\r\n", csv_enc(str, 0, &trash)); |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2026 | * |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 2027 | * If <quote> is 1, the converter puts the quotes only if any reserved character |
| 2028 | * is present. If <quote> is 2, the converter always puts the quotes. |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2029 | * |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2030 | * <output> is a struct buffer used for storing the output string. |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2031 | * |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2032 | * The function returns the converted string on its output. If an error |
| 2033 | * 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] | 2034 | * for using the function directly as printf() argument. |
| 2035 | * |
| 2036 | * If the output buffer is too short to contain the input string, the result |
| 2037 | * is truncated. |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2038 | * |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 2039 | * This function appends the encoding to the existing output chunk, and it |
| 2040 | * guarantees that it starts immediately at the first available character of |
| 2041 | * the chunk. Please use csv_enc() instead if you want to replace the output |
| 2042 | * chunk. |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2043 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 2044 | 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] | 2045 | { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2046 | char *end = output->area + output->size; |
| 2047 | char *out = output->area + output->data; |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2048 | char *ptr = out; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2049 | |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 2050 | if (quote == 1) { |
| 2051 | /* automatic quoting: first verify if we'll have to quote the string */ |
| 2052 | if (!strpbrk(str, "\n\r,\"")) |
| 2053 | quote = 0; |
| 2054 | } |
| 2055 | |
| 2056 | if (quote) |
| 2057 | *ptr++ = '"'; |
| 2058 | |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2059 | while (*str && ptr < end - 2) { /* -2 for reserving space for <"> and \0. */ |
| 2060 | *ptr = *str; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2061 | if (*str == '"') { |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2062 | ptr++; |
| 2063 | if (ptr >= end - 2) { |
| 2064 | ptr--; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2065 | break; |
| 2066 | } |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2067 | *ptr = '"'; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2068 | } |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2069 | ptr++; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2070 | str++; |
| 2071 | } |
| 2072 | |
Willy Tarreau | b631c29 | 2016-01-08 10:04:08 +0100 | [diff] [blame] | 2073 | if (quote) |
| 2074 | *ptr++ = '"'; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2075 | |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2076 | *ptr = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2077 | output->data = ptr - output->area; |
Willy Tarreau | 898529b | 2016-01-06 18:07:04 +0100 | [diff] [blame] | 2078 | return out; |
Thierry FOURNIER | ddea626 | 2015-05-28 16:00:28 +0200 | [diff] [blame] | 2079 | } |
| 2080 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2081 | /* Decode an URL-encoded string in-place. The resulting string might |
| 2082 | * be shorter. If some forbidden characters are found, the conversion is |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 2083 | * aborted, the string is truncated before the issue and a negative value is |
| 2084 | * returned, otherwise the operation returns the length of the decoded string. |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 2085 | * If the 'in_form' argument is non-nul the string is assumed to be part of |
| 2086 | * an "application/x-www-form-urlencoded" encoded string, and the '+' will be |
| 2087 | * turned to a space. If it's zero, this will only be done after a question |
| 2088 | * mark ('?'). |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2089 | */ |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 2090 | int url_decode(char *string, int in_form) |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2091 | { |
| 2092 | char *in, *out; |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 2093 | int ret = -1; |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2094 | |
| 2095 | in = string; |
| 2096 | out = string; |
| 2097 | while (*in) { |
| 2098 | switch (*in) { |
| 2099 | case '+' : |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 2100 | *out++ = in_form ? ' ' : *in; |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2101 | break; |
| 2102 | case '%' : |
| 2103 | if (!ishex(in[1]) || !ishex(in[2])) |
| 2104 | goto end; |
| 2105 | *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]); |
| 2106 | in += 2; |
| 2107 | break; |
Willy Tarreau | 62ba9ba | 2020-04-23 17:54:47 +0200 | [diff] [blame] | 2108 | case '?': |
| 2109 | in_form = 1; |
Willy Tarreau | 7de8de0 | 2022-11-14 07:20:09 +0100 | [diff] [blame] | 2110 | __fallthrough; |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2111 | default: |
| 2112 | *out++ = *in; |
| 2113 | break; |
| 2114 | } |
| 2115 | in++; |
| 2116 | } |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 2117 | ret = out - string; /* success */ |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 2118 | end: |
| 2119 | *out = 0; |
| 2120 | return ret; |
| 2121 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2122 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2123 | unsigned int str2ui(const char *s) |
| 2124 | { |
| 2125 | return __str2ui(s); |
| 2126 | } |
| 2127 | |
| 2128 | unsigned int str2uic(const char *s) |
| 2129 | { |
| 2130 | return __str2uic(s); |
| 2131 | } |
| 2132 | |
| 2133 | unsigned int strl2ui(const char *s, int len) |
| 2134 | { |
| 2135 | return __strl2ui(s, len); |
| 2136 | } |
| 2137 | |
| 2138 | unsigned int strl2uic(const char *s, int len) |
| 2139 | { |
| 2140 | return __strl2uic(s, len); |
| 2141 | } |
| 2142 | |
Willy Tarreau | 4ec83cd | 2010-10-15 23:19:55 +0200 | [diff] [blame] | 2143 | unsigned int read_uint(const char **s, const char *end) |
| 2144 | { |
| 2145 | return __read_uint(s, end); |
| 2146 | } |
| 2147 | |
Thierry FOURNIER | 763a5d8 | 2015-07-06 23:09:52 +0200 | [diff] [blame] | 2148 | /* This function reads an unsigned integer from the string pointed to by <s> and |
| 2149 | * returns it. The <s> pointer is adjusted to point to the first unread char. The |
| 2150 | * function automatically stops at <end>. If the number overflows, the 2^64-1 |
| 2151 | * value is returned. |
| 2152 | */ |
| 2153 | unsigned long long int read_uint64(const char **s, const char *end) |
| 2154 | { |
| 2155 | const char *ptr = *s; |
| 2156 | unsigned long long int i = 0, tmp; |
| 2157 | unsigned int j; |
| 2158 | |
| 2159 | while (ptr < end) { |
| 2160 | |
| 2161 | /* read next char */ |
| 2162 | j = *ptr - '0'; |
| 2163 | if (j > 9) |
| 2164 | goto read_uint64_end; |
| 2165 | |
| 2166 | /* add char to the number and check overflow. */ |
| 2167 | tmp = i * 10; |
| 2168 | if (tmp / 10 != i) { |
| 2169 | i = ULLONG_MAX; |
| 2170 | goto read_uint64_eat; |
| 2171 | } |
| 2172 | if (ULLONG_MAX - tmp < j) { |
| 2173 | i = ULLONG_MAX; |
| 2174 | goto read_uint64_eat; |
| 2175 | } |
| 2176 | i = tmp + j; |
| 2177 | ptr++; |
| 2178 | } |
| 2179 | read_uint64_eat: |
| 2180 | /* eat each numeric char */ |
| 2181 | while (ptr < end) { |
| 2182 | if ((unsigned int)(*ptr - '0') > 9) |
| 2183 | break; |
| 2184 | ptr++; |
| 2185 | } |
| 2186 | read_uint64_end: |
| 2187 | *s = ptr; |
| 2188 | return i; |
| 2189 | } |
| 2190 | |
| 2191 | /* This function reads an integer from the string pointed to by <s> and returns |
| 2192 | * it. The <s> pointer is adjusted to point to the first unread char. The function |
| 2193 | * automatically stops at <end>. Il the number is bigger than 2^63-2, the 2^63-1 |
| 2194 | * value is returned. If the number is lowest than -2^63-1, the -2^63 value is |
| 2195 | * returned. |
| 2196 | */ |
| 2197 | long long int read_int64(const char **s, const char *end) |
| 2198 | { |
| 2199 | unsigned long long int i = 0; |
| 2200 | int neg = 0; |
| 2201 | |
| 2202 | /* Look for minus char. */ |
| 2203 | if (**s == '-') { |
| 2204 | neg = 1; |
| 2205 | (*s)++; |
| 2206 | } |
| 2207 | else if (**s == '+') |
| 2208 | (*s)++; |
| 2209 | |
| 2210 | /* convert as positive number. */ |
| 2211 | i = read_uint64(s, end); |
| 2212 | |
| 2213 | if (neg) { |
| 2214 | if (i > 0x8000000000000000ULL) |
| 2215 | return LLONG_MIN; |
| 2216 | return -i; |
| 2217 | } |
| 2218 | if (i > 0x7fffffffffffffffULL) |
| 2219 | return LLONG_MAX; |
| 2220 | return i; |
| 2221 | } |
| 2222 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2223 | /* This one is 7 times faster than strtol() on athlon with checks. |
| 2224 | * It returns the value of the number composed of all valid digits read, |
| 2225 | * and can process negative numbers too. |
| 2226 | */ |
| 2227 | int strl2ic(const char *s, int len) |
| 2228 | { |
| 2229 | int i = 0; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2230 | int j, k; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2231 | |
| 2232 | if (len > 0) { |
| 2233 | if (*s != '-') { |
| 2234 | /* positive number */ |
| 2235 | while (len-- > 0) { |
| 2236 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2237 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2238 | if (j > 9) |
| 2239 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2240 | i = k + j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2241 | } |
| 2242 | } else { |
| 2243 | /* negative number */ |
| 2244 | s++; |
| 2245 | while (--len > 0) { |
| 2246 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2247 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2248 | if (j > 9) |
| 2249 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 2250 | i = k - j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2251 | } |
| 2252 | } |
| 2253 | } |
| 2254 | return i; |
| 2255 | } |
| 2256 | |
| 2257 | |
| 2258 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 2259 | * signed integer which it stores into <ret>. It accurately detects any error |
| 2260 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 2261 | * applications designed for hostile environments. It returns zero when the |
| 2262 | * number has successfully been converted, non-zero otherwise. When an error |
| 2263 | * is returned, the <ret> value is left untouched. It is yet 5 to 40 times |
| 2264 | * faster than strtol(). |
| 2265 | */ |
| 2266 | int strl2irc(const char *s, int len, int *ret) |
| 2267 | { |
| 2268 | int i = 0; |
| 2269 | int j; |
| 2270 | |
| 2271 | if (!len) |
| 2272 | return 1; |
| 2273 | |
| 2274 | if (*s != '-') { |
| 2275 | /* positive number */ |
| 2276 | while (len-- > 0) { |
| 2277 | j = (*s++) - '0'; |
| 2278 | if (j > 9) return 1; /* invalid char */ |
| 2279 | if (i > INT_MAX / 10) return 1; /* check for multiply overflow */ |
| 2280 | i = i * 10; |
| 2281 | if (i + j < i) return 1; /* check for addition overflow */ |
| 2282 | i = i + j; |
| 2283 | } |
| 2284 | } else { |
| 2285 | /* negative number */ |
| 2286 | s++; |
| 2287 | while (--len > 0) { |
| 2288 | j = (*s++) - '0'; |
| 2289 | if (j > 9) return 1; /* invalid char */ |
| 2290 | if (i < INT_MIN / 10) return 1; /* check for multiply overflow */ |
| 2291 | i = i * 10; |
| 2292 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 2293 | i = i - j; |
| 2294 | } |
| 2295 | } |
| 2296 | *ret = i; |
| 2297 | return 0; |
| 2298 | } |
| 2299 | |
| 2300 | |
| 2301 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 2302 | * signed integer which it stores into <ret>. It accurately detects any error |
| 2303 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 2304 | * applications designed for hostile environments. It returns zero when the |
| 2305 | * number has successfully been converted, non-zero otherwise. When an error |
| 2306 | * 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] | 2307 | * than strl2irc(). |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2308 | */ |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2309 | |
| 2310 | int strl2llrc(const char *s, int len, long long *ret) |
| 2311 | { |
| 2312 | long long i = 0; |
| 2313 | int j; |
| 2314 | |
| 2315 | if (!len) |
| 2316 | return 1; |
| 2317 | |
| 2318 | if (*s != '-') { |
| 2319 | /* positive number */ |
| 2320 | while (len-- > 0) { |
| 2321 | j = (*s++) - '0'; |
| 2322 | if (j > 9) return 1; /* invalid char */ |
| 2323 | if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */ |
| 2324 | i = i * 10LL; |
| 2325 | if (i + j < i) return 1; /* check for addition overflow */ |
| 2326 | i = i + j; |
| 2327 | } |
| 2328 | } else { |
| 2329 | /* negative number */ |
| 2330 | s++; |
| 2331 | while (--len > 0) { |
| 2332 | j = (*s++) - '0'; |
| 2333 | if (j > 9) return 1; /* invalid char */ |
| 2334 | if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */ |
| 2335 | i = i * 10LL; |
| 2336 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 2337 | i = i - j; |
| 2338 | } |
| 2339 | } |
| 2340 | *ret = i; |
| 2341 | return 0; |
| 2342 | } |
| 2343 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 2344 | /* This function is used with pat_parse_dotted_ver(). It converts a string |
| 2345 | * composed by two number separated by a dot. Each part must contain in 16 bits |
| 2346 | * because internally they will be represented as a 32-bit quantity stored in |
| 2347 | * a 64-bit integer. It returns zero when the number has successfully been |
| 2348 | * converted, non-zero otherwise. When an error is returned, the <ret> value |
| 2349 | * is left untouched. |
| 2350 | * |
| 2351 | * "1.3" -> 0x0000000000010003 |
| 2352 | * "65535.65535" -> 0x00000000ffffffff |
| 2353 | */ |
| 2354 | int strl2llrc_dotted(const char *text, int len, long long *ret) |
| 2355 | { |
| 2356 | const char *end = &text[len]; |
| 2357 | const char *p; |
| 2358 | long long major, minor; |
| 2359 | |
| 2360 | /* Look for dot. */ |
| 2361 | for (p = text; p < end; p++) |
| 2362 | if (*p == '.') |
| 2363 | break; |
| 2364 | |
| 2365 | /* Convert major. */ |
| 2366 | if (strl2llrc(text, p - text, &major) != 0) |
| 2367 | return 1; |
| 2368 | |
| 2369 | /* Check major. */ |
| 2370 | if (major >= 65536) |
| 2371 | return 1; |
| 2372 | |
| 2373 | /* Convert minor. */ |
| 2374 | minor = 0; |
| 2375 | if (p < end) |
| 2376 | if (strl2llrc(p + 1, end - (p + 1), &minor) != 0) |
| 2377 | return 1; |
| 2378 | |
| 2379 | /* Check minor. */ |
| 2380 | if (minor >= 65536) |
| 2381 | return 1; |
| 2382 | |
| 2383 | /* Compose value. */ |
| 2384 | *ret = (major << 16) | (minor & 0xffff); |
| 2385 | return 0; |
| 2386 | } |
| 2387 | |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2388 | /* This function parses a time value optionally followed by a unit suffix among |
| 2389 | * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit |
| 2390 | * expected by the caller. The computation does its best to avoid overflows. |
| 2391 | * The value is returned in <ret> if everything is fine, and a NULL is returned |
| 2392 | * by the function. In case of error, a pointer to the error is returned and |
| 2393 | * <ret> is left untouched. Values are automatically rounded up when needed. |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2394 | * Values resulting in values larger than or equal to 2^31 after conversion are |
| 2395 | * reported as an overflow as value PARSE_TIME_OVER. Non-null values resulting |
| 2396 | * 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] | 2397 | */ |
| 2398 | const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags) |
| 2399 | { |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2400 | unsigned long long imult, idiv; |
| 2401 | unsigned long long omult, odiv; |
| 2402 | unsigned long long value, result; |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2403 | const char *str = text; |
| 2404 | |
| 2405 | if (!isdigit((unsigned char)*text)) |
| 2406 | return text; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2407 | |
| 2408 | omult = odiv = 1; |
| 2409 | |
| 2410 | switch (unit_flags & TIME_UNIT_MASK) { |
| 2411 | case TIME_UNIT_US: omult = 1000000; break; |
| 2412 | case TIME_UNIT_MS: omult = 1000; break; |
| 2413 | case TIME_UNIT_S: break; |
| 2414 | case TIME_UNIT_MIN: odiv = 60; break; |
| 2415 | case TIME_UNIT_HOUR: odiv = 3600; break; |
| 2416 | case TIME_UNIT_DAY: odiv = 86400; break; |
| 2417 | default: break; |
| 2418 | } |
| 2419 | |
| 2420 | value = 0; |
| 2421 | |
| 2422 | while (1) { |
| 2423 | unsigned int j; |
| 2424 | |
| 2425 | j = *text - '0'; |
| 2426 | if (j > 9) |
| 2427 | break; |
| 2428 | text++; |
| 2429 | value *= 10; |
| 2430 | value += j; |
| 2431 | } |
| 2432 | |
| 2433 | imult = idiv = 1; |
| 2434 | switch (*text) { |
| 2435 | case '\0': /* no unit = default unit */ |
| 2436 | imult = omult = idiv = odiv = 1; |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2437 | goto end; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2438 | case 's': /* second = unscaled unit */ |
| 2439 | break; |
| 2440 | case 'u': /* microsecond : "us" */ |
| 2441 | if (text[1] == 's') { |
| 2442 | idiv = 1000000; |
| 2443 | text++; |
Thayne McCombs | a683805 | 2021-04-02 14:12:43 -0600 | [diff] [blame] | 2444 | break; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2445 | } |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2446 | return text; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2447 | case 'm': /* millisecond : "ms" or minute: "m" */ |
| 2448 | if (text[1] == 's') { |
| 2449 | idiv = 1000; |
| 2450 | text++; |
| 2451 | } else |
| 2452 | imult = 60; |
| 2453 | break; |
| 2454 | case 'h': /* hour : "h" */ |
| 2455 | imult = 3600; |
| 2456 | break; |
| 2457 | case 'd': /* day : "d" */ |
| 2458 | imult = 86400; |
| 2459 | break; |
| 2460 | default: |
| 2461 | return text; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2462 | } |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2463 | if (*(++text) != '\0') { |
| 2464 | ha_warning("unexpected character '%c' after the timer value '%s', only " |
| 2465 | "(us=microseconds,ms=milliseconds,s=seconds,m=minutes,h=hours,d=days) are supported." |
| 2466 | " This will be reported as an error in next versions.\n", *text, str); |
| 2467 | } |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2468 | |
Christopher Faulet | c20ad0d | 2020-12-11 09:23:07 +0100 | [diff] [blame] | 2469 | end: |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2470 | if (omult % idiv == 0) { omult /= idiv; idiv = 1; } |
| 2471 | if (idiv % omult == 0) { idiv /= omult; omult = 1; } |
| 2472 | if (imult % odiv == 0) { imult /= odiv; odiv = 1; } |
| 2473 | if (odiv % imult == 0) { odiv /= imult; imult = 1; } |
| 2474 | |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 2475 | result = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv); |
| 2476 | if (result >= 0x80000000) |
| 2477 | return PARSE_TIME_OVER; |
| 2478 | if (!result && value) |
| 2479 | return PARSE_TIME_UNDER; |
| 2480 | *ret = result; |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 2481 | return NULL; |
| 2482 | } |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 2483 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2484 | /* this function converts the string starting at <text> to an unsigned int |
| 2485 | * 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] | 2486 | * character is returned. If the conversion is successful, NULL is returned. |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2487 | */ |
| 2488 | const char *parse_size_err(const char *text, unsigned *ret) { |
| 2489 | unsigned value = 0; |
| 2490 | |
Christopher Faulet | 82635a0 | 2020-12-11 09:30:45 +0100 | [diff] [blame] | 2491 | if (!isdigit((unsigned char)*text)) |
| 2492 | return text; |
| 2493 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2494 | while (1) { |
| 2495 | unsigned int j; |
| 2496 | |
| 2497 | j = *text - '0'; |
| 2498 | if (j > 9) |
| 2499 | break; |
| 2500 | if (value > ~0U / 10) |
| 2501 | return text; |
| 2502 | value *= 10; |
| 2503 | if (value > (value + j)) |
| 2504 | return text; |
| 2505 | value += j; |
| 2506 | text++; |
| 2507 | } |
| 2508 | |
| 2509 | switch (*text) { |
| 2510 | case '\0': |
| 2511 | break; |
| 2512 | case 'K': |
| 2513 | case 'k': |
| 2514 | if (value > ~0U >> 10) |
| 2515 | return text; |
| 2516 | value = value << 10; |
| 2517 | break; |
| 2518 | case 'M': |
| 2519 | case 'm': |
| 2520 | if (value > ~0U >> 20) |
| 2521 | return text; |
| 2522 | value = value << 20; |
| 2523 | break; |
| 2524 | case 'G': |
| 2525 | case 'g': |
| 2526 | if (value > ~0U >> 30) |
| 2527 | return text; |
| 2528 | value = value << 30; |
| 2529 | break; |
| 2530 | default: |
| 2531 | return text; |
| 2532 | } |
| 2533 | |
Godbach | 58048a2 | 2015-01-28 17:36:16 +0800 | [diff] [blame] | 2534 | if (*text != '\0' && *++text != '\0') |
| 2535 | return text; |
| 2536 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 2537 | *ret = value; |
| 2538 | return NULL; |
| 2539 | } |
| 2540 | |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2541 | /* |
| 2542 | * Parse binary string written in hexadecimal (source) and store the decoded |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2543 | * 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] | 2544 | * 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] | 2545 | * error message in err. In success case, it returns the consumed length. |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2546 | */ |
| 2547 | int parse_binary(const char *source, char **binstr, int *binstrlen, char **err) |
| 2548 | { |
| 2549 | int len; |
| 2550 | const char *p = source; |
| 2551 | int i,j; |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2552 | int alloc; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2553 | |
| 2554 | len = strlen(source); |
| 2555 | if (len % 2) { |
| 2556 | memprintf(err, "an even number of hex digit is expected"); |
| 2557 | return 0; |
| 2558 | } |
| 2559 | |
| 2560 | len = len >> 1; |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2561 | |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2562 | if (!*binstr) { |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 2563 | *binstr = calloc(len, sizeof(**binstr)); |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2564 | if (!*binstr) { |
| 2565 | memprintf(err, "out of memory while loading string pattern"); |
| 2566 | return 0; |
| 2567 | } |
| 2568 | alloc = 1; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2569 | } |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2570 | else { |
| 2571 | if (*binstrlen < len) { |
Joseph Herlant | 76dbe78 | 2018-11-15 12:01:22 -0800 | [diff] [blame] | 2572 | memprintf(err, "no space available in the buffer. expect %d, provides %d", |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 2573 | len, *binstrlen); |
| 2574 | return 0; |
| 2575 | } |
| 2576 | alloc = 0; |
| 2577 | } |
| 2578 | *binstrlen = len; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2579 | |
| 2580 | i = j = 0; |
| 2581 | while (j < len) { |
| 2582 | if (!ishex(p[i++])) |
| 2583 | goto bad_input; |
| 2584 | if (!ishex(p[i++])) |
| 2585 | goto bad_input; |
| 2586 | (*binstr)[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]); |
| 2587 | } |
Thierry FOURNIER | ee330af | 2014-01-21 11:36:14 +0100 | [diff] [blame] | 2588 | return len << 1; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2589 | |
| 2590 | bad_input: |
| 2591 | memprintf(err, "an hex digit is expected (found '%c')", p[i-1]); |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 2592 | if (alloc) |
| 2593 | ha_free(binstr); |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 2594 | return 0; |
| 2595 | } |
| 2596 | |
Willy Tarreau | 946ba59 | 2009-05-10 15:41:18 +0200 | [diff] [blame] | 2597 | /* copies at most <n> characters from <src> and always terminates with '\0' */ |
| 2598 | char *my_strndup(const char *src, int n) |
| 2599 | { |
| 2600 | int len = 0; |
| 2601 | char *ret; |
| 2602 | |
| 2603 | while (len < n && src[len]) |
| 2604 | len++; |
| 2605 | |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2606 | ret = malloc(len + 1); |
Willy Tarreau | 946ba59 | 2009-05-10 15:41:18 +0200 | [diff] [blame] | 2607 | if (!ret) |
| 2608 | return ret; |
| 2609 | memcpy(ret, src, len); |
| 2610 | ret[len] = '\0'; |
| 2611 | return ret; |
| 2612 | } |
| 2613 | |
Baptiste Assmann | bb77c8e | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 2614 | /* |
| 2615 | * search needle in haystack |
| 2616 | * returns the pointer if found, returns NULL otherwise |
| 2617 | */ |
| 2618 | const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) |
| 2619 | { |
| 2620 | const void *c = NULL; |
| 2621 | unsigned char f; |
| 2622 | |
| 2623 | if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen)) |
| 2624 | return NULL; |
| 2625 | |
| 2626 | f = *(char *)needle; |
| 2627 | c = haystack; |
| 2628 | while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) { |
| 2629 | if ((haystacklen - (c - haystack)) < needlelen) |
| 2630 | return NULL; |
| 2631 | |
| 2632 | if (memcmp(c, needle, needlelen) == 0) |
| 2633 | return c; |
| 2634 | ++c; |
| 2635 | } |
| 2636 | return NULL; |
| 2637 | } |
| 2638 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 2639 | /* get length of the initial segment consisting entirely of bytes in <accept> */ |
Christopher Faulet | 5eb96cb | 2020-04-15 10:23:01 +0200 | [diff] [blame] | 2640 | size_t my_memspn(const void *str, size_t len, const void *accept, size_t acceptlen) |
| 2641 | { |
| 2642 | size_t ret = 0; |
| 2643 | |
| 2644 | while (ret < len && memchr(accept, *((int *)str), acceptlen)) { |
| 2645 | str++; |
| 2646 | ret++; |
| 2647 | } |
| 2648 | return ret; |
| 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 not in <rejcet> */ |
Christopher Faulet | 5eb96cb | 2020-04-15 10:23:01 +0200 | [diff] [blame] | 2652 | size_t my_memcspn(const void *str, size_t len, const void *reject, size_t rejectlen) |
| 2653 | { |
| 2654 | size_t ret = 0; |
| 2655 | |
| 2656 | while (ret < len) { |
| 2657 | if(memchr(reject, *((int *)str), rejectlen)) |
| 2658 | return ret; |
| 2659 | str++; |
| 2660 | ret++; |
| 2661 | } |
| 2662 | return ret; |
| 2663 | } |
| 2664 | |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 2665 | /* This function returns the first unused key greater than or equal to <key> in |
| 2666 | * ID tree <root>. Zero is returned if no place is found. |
| 2667 | */ |
| 2668 | unsigned int get_next_id(struct eb_root *root, unsigned int key) |
| 2669 | { |
| 2670 | struct eb32_node *used; |
| 2671 | |
| 2672 | do { |
| 2673 | used = eb32_lookup_ge(root, key); |
| 2674 | if (!used || used->key > key) |
| 2675 | return key; /* key is available */ |
| 2676 | key++; |
| 2677 | } while (key); |
| 2678 | return key; |
| 2679 | } |
| 2680 | |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2681 | /* dump the full tree to <file> in DOT format for debugging purposes. Will |
| 2682 | * optionally highlight node <subj> if found, depending on operation <op> : |
| 2683 | * 0 : nothing |
| 2684 | * >0 : insertion, node/leaf are surrounded in red |
| 2685 | * <0 : removal, node/leaf are dashed with no background |
| 2686 | * Will optionally add "desc" as a label on the graph if set and non-null. |
| 2687 | */ |
| 2688 | 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] | 2689 | { |
| 2690 | struct eb32sc_node *node; |
| 2691 | unsigned long scope = -1; |
| 2692 | |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2693 | fprintf(file, "digraph ebtree {\n"); |
| 2694 | |
| 2695 | if (desc && *desc) { |
| 2696 | fprintf(file, |
| 2697 | " fontname=\"fixed\";\n" |
| 2698 | " fontsize=8;\n" |
| 2699 | " label=\"%s\";\n", desc); |
| 2700 | } |
| 2701 | |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2702 | fprintf(file, |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2703 | " node [fontname=\"fixed\" fontsize=8 shape=\"box\" style=\"filled\" color=\"black\" fillcolor=\"white\"];\n" |
| 2704 | " edge [fontname=\"fixed\" fontsize=8 style=\"solid\" color=\"magenta\" dir=\"forward\"];\n" |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2705 | " \"%lx_n\" [label=\"root\\n%lx\"]\n", (long)eb_root_to_node(root), (long)root |
| 2706 | ); |
| 2707 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2708 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"L\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2709 | (long)eb_root_to_node(root), |
| 2710 | (long)eb_root_to_node(eb_clrtag(root->b[0])), |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2711 | eb_gettag(root->b[0]) == EB_LEAF ? 'l' : 'n'); |
| 2712 | |
| 2713 | node = eb32sc_first(root, scope); |
| 2714 | while (node) { |
| 2715 | if (node->node.node_p) { |
| 2716 | /* node part is used */ |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2717 | fprintf(file, " \"%lx_n\" [label=\"%lx\\nkey=%u\\nscope=%lx\\nbit=%d\" fillcolor=\"lightskyblue1\" %s];\n", |
| 2718 | (long)node, (long)node, node->key, node->node_s, node->node.bit, |
| 2719 | (node == subj) ? (op < 0 ? "color=\"red\" style=\"dashed\"" : op > 0 ? "color=\"red\"" : "") : ""); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2720 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2721 | fprintf(file, " \"%lx_n\" -> \"%lx_n\" [taillabel=\"%c\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2722 | (long)node, |
| 2723 | (long)eb_root_to_node(eb_clrtag(node->node.node_p)), |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2724 | eb_gettag(node->node.node_p) ? 'R' : 'L'); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2725 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2726 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"L\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2727 | (long)node, |
| 2728 | (long)eb_root_to_node(eb_clrtag(node->node.branches.b[0])), |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2729 | eb_gettag(node->node.branches.b[0]) == EB_LEAF ? 'l' : 'n'); |
| 2730 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2731 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"R\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2732 | (long)node, |
| 2733 | (long)eb_root_to_node(eb_clrtag(node->node.branches.b[1])), |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2734 | eb_gettag(node->node.branches.b[1]) == EB_LEAF ? 'l' : 'n'); |
| 2735 | } |
| 2736 | |
Willy Tarreau | 9c1e15d | 2017-11-15 18:51:29 +0100 | [diff] [blame] | 2737 | fprintf(file, " \"%lx_l\" [label=\"%lx\\nkey=%u\\nscope=%lx\\npfx=%u\" fillcolor=\"yellow\" %s];\n", |
| 2738 | (long)node, (long)node, node->key, node->leaf_s, node->node.pfx, |
| 2739 | (node == subj) ? (op < 0 ? "color=\"red\" style=\"dashed\"" : op > 0 ? "color=\"red\"" : "") : ""); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2740 | |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2741 | fprintf(file, " \"%lx_l\" -> \"%lx_n\" [taillabel=\"%c\"];\n", |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2742 | (long)node, |
| 2743 | (long)eb_root_to_node(eb_clrtag(node->node.leaf_p)), |
Willy Tarreau | 6c7f4de | 2017-11-15 17:49:54 +0100 | [diff] [blame] | 2744 | eb_gettag(node->node.leaf_p) ? 'R' : 'L'); |
Willy Tarreau | ed3cda0 | 2017-11-15 15:04:05 +0100 | [diff] [blame] | 2745 | node = eb32sc_next(node, scope); |
| 2746 | } |
| 2747 | fprintf(file, "}\n"); |
| 2748 | } |
| 2749 | |
Willy Tarreau | 0dc9e6d | 2022-08-01 11:55:57 +0200 | [diff] [blame] | 2750 | /* dump the full tree to <file> in DOT format for debugging purposes. Will |
| 2751 | * optionally highlight node <subj> if found, depending on operation <op> : |
| 2752 | * 0 : nothing |
| 2753 | * >0 : insertion, node/leaf are surrounded in red |
| 2754 | * <0 : removal, node/leaf are dashed with no background |
| 2755 | * Will optionally add "desc" as a label on the graph if set and non-null. The |
| 2756 | * key is printed as a u32 hex value. A full-sized hex dump would be better but |
| 2757 | * is left to be implemented. |
| 2758 | */ |
| 2759 | void ebmb_to_file(FILE *file, struct eb_root *root, const struct ebmb_node *subj, int op, const char *desc) |
| 2760 | { |
| 2761 | struct ebmb_node *node; |
| 2762 | |
| 2763 | fprintf(file, "digraph ebtree {\n"); |
| 2764 | |
| 2765 | if (desc && *desc) { |
| 2766 | fprintf(file, |
| 2767 | " fontname=\"fixed\";\n" |
| 2768 | " fontsize=8;\n" |
| 2769 | " label=\"%s\";\n", desc); |
| 2770 | } |
| 2771 | |
| 2772 | fprintf(file, |
| 2773 | " node [fontname=\"fixed\" fontsize=8 shape=\"box\" style=\"filled\" color=\"black\" fillcolor=\"white\"];\n" |
| 2774 | " edge [fontname=\"fixed\" fontsize=8 style=\"solid\" color=\"magenta\" dir=\"forward\"];\n" |
| 2775 | " \"%lx_n\" [label=\"root\\n%lx\"]\n", (long)eb_root_to_node(root), (long)root |
| 2776 | ); |
| 2777 | |
| 2778 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"L\"];\n", |
| 2779 | (long)eb_root_to_node(root), |
| 2780 | (long)eb_root_to_node(eb_clrtag(root->b[0])), |
| 2781 | eb_gettag(root->b[0]) == EB_LEAF ? 'l' : 'n'); |
| 2782 | |
| 2783 | node = ebmb_first(root); |
| 2784 | while (node) { |
| 2785 | if (node->node.node_p) { |
| 2786 | /* node part is used */ |
| 2787 | fprintf(file, " \"%lx_n\" [label=\"%lx\\nkey=%#x\\nbit=%d\" fillcolor=\"lightskyblue1\" %s];\n", |
| 2788 | (long)node, (long)node, read_u32(node->key), node->node.bit, |
| 2789 | (node == subj) ? (op < 0 ? "color=\"red\" style=\"dashed\"" : op > 0 ? "color=\"red\"" : "") : ""); |
| 2790 | |
| 2791 | fprintf(file, " \"%lx_n\" -> \"%lx_n\" [taillabel=\"%c\"];\n", |
| 2792 | (long)node, |
| 2793 | (long)eb_root_to_node(eb_clrtag(node->node.node_p)), |
| 2794 | eb_gettag(node->node.node_p) ? 'R' : 'L'); |
| 2795 | |
| 2796 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"L\"];\n", |
| 2797 | (long)node, |
| 2798 | (long)eb_root_to_node(eb_clrtag(node->node.branches.b[0])), |
| 2799 | eb_gettag(node->node.branches.b[0]) == EB_LEAF ? 'l' : 'n'); |
| 2800 | |
| 2801 | fprintf(file, " \"%lx_n\" -> \"%lx_%c\" [taillabel=\"R\"];\n", |
| 2802 | (long)node, |
| 2803 | (long)eb_root_to_node(eb_clrtag(node->node.branches.b[1])), |
| 2804 | eb_gettag(node->node.branches.b[1]) == EB_LEAF ? 'l' : 'n'); |
| 2805 | } |
| 2806 | |
| 2807 | fprintf(file, " \"%lx_l\" [label=\"%lx\\nkey=%#x\\npfx=%u\" fillcolor=\"yellow\" %s];\n", |
| 2808 | (long)node, (long)node, read_u32(node->key), node->node.pfx, |
| 2809 | (node == subj) ? (op < 0 ? "color=\"red\" style=\"dashed\"" : op > 0 ? "color=\"red\"" : "") : ""); |
| 2810 | |
| 2811 | fprintf(file, " \"%lx_l\" -> \"%lx_n\" [taillabel=\"%c\"];\n", |
| 2812 | (long)node, |
| 2813 | (long)eb_root_to_node(eb_clrtag(node->node.leaf_p)), |
| 2814 | eb_gettag(node->node.leaf_p) ? 'R' : 'L'); |
| 2815 | node = ebmb_next(node); |
| 2816 | } |
| 2817 | fprintf(file, "}\n"); |
| 2818 | } |
| 2819 | |
Willy Tarreau | 348238b | 2010-01-18 15:05:57 +0100 | [diff] [blame] | 2820 | /* This function compares a sample word possibly followed by blanks to another |
| 2821 | * clean word. The compare is case-insensitive. 1 is returned if both are equal, |
| 2822 | * otherwise zero. This intends to be used when checking HTTP headers for some |
| 2823 | * values. Note that it validates a word followed only by blanks but does not |
| 2824 | * validate a word followed by blanks then other chars. |
| 2825 | */ |
| 2826 | int word_match(const char *sample, int slen, const char *word, int wlen) |
| 2827 | { |
| 2828 | if (slen < wlen) |
| 2829 | return 0; |
| 2830 | |
| 2831 | while (wlen) { |
| 2832 | char c = *sample ^ *word; |
| 2833 | if (c && c != ('A' ^ 'a')) |
| 2834 | return 0; |
| 2835 | sample++; |
| 2836 | word++; |
| 2837 | slen--; |
| 2838 | wlen--; |
| 2839 | } |
| 2840 | |
| 2841 | while (slen) { |
| 2842 | if (*sample != ' ' && *sample != '\t') |
| 2843 | return 0; |
| 2844 | sample++; |
| 2845 | slen--; |
| 2846 | } |
| 2847 | return 1; |
| 2848 | } |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 2849 | |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2850 | /* Converts any text-formatted IPv4 address to a host-order IPv4 address. It |
| 2851 | * is particularly fast because it avoids expensive operations such as |
| 2852 | * multiplies, which are optimized away at the end. It requires a properly |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2853 | * formatted address though (3 points). |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2854 | */ |
| 2855 | unsigned int inetaddr_host(const char *text) |
| 2856 | { |
| 2857 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 2858 | register unsigned int dig100, dig10, dig1; |
| 2859 | int s; |
| 2860 | const char *p, *d; |
| 2861 | |
| 2862 | dig1 = dig10 = dig100 = ascii_zero; |
| 2863 | s = 24; |
| 2864 | |
| 2865 | p = text; |
| 2866 | while (1) { |
| 2867 | if (((unsigned)(*p - '0')) <= 9) { |
| 2868 | p++; |
| 2869 | continue; |
| 2870 | } |
| 2871 | |
| 2872 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 2873 | if (p == text) |
| 2874 | goto end; |
| 2875 | |
| 2876 | d = p - 1; |
| 2877 | dig1 |= (unsigned int)(*d << s); |
| 2878 | if (d == text) |
| 2879 | goto end; |
| 2880 | |
| 2881 | d--; |
| 2882 | dig10 |= (unsigned int)(*d << s); |
| 2883 | if (d == text) |
| 2884 | goto end; |
| 2885 | |
| 2886 | d--; |
| 2887 | dig100 |= (unsigned int)(*d << s); |
| 2888 | end: |
| 2889 | if (!s || *p != '.') |
| 2890 | break; |
| 2891 | |
| 2892 | s -= 8; |
| 2893 | text = ++p; |
| 2894 | } |
| 2895 | |
| 2896 | dig100 -= ascii_zero; |
| 2897 | dig10 -= ascii_zero; |
| 2898 | dig1 -= ascii_zero; |
| 2899 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 2900 | } |
| 2901 | |
| 2902 | /* |
| 2903 | * Idem except the first unparsed character has to be passed in <stop>. |
| 2904 | */ |
| 2905 | unsigned int inetaddr_host_lim(const char *text, const char *stop) |
| 2906 | { |
| 2907 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 2908 | register unsigned int dig100, dig10, dig1; |
| 2909 | int s; |
| 2910 | const char *p, *d; |
| 2911 | |
| 2912 | dig1 = dig10 = dig100 = ascii_zero; |
| 2913 | s = 24; |
| 2914 | |
| 2915 | p = text; |
| 2916 | while (1) { |
| 2917 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 2918 | p++; |
| 2919 | continue; |
| 2920 | } |
| 2921 | |
| 2922 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 2923 | if (p == text) |
| 2924 | goto end; |
| 2925 | |
| 2926 | d = p - 1; |
| 2927 | dig1 |= (unsigned int)(*d << s); |
| 2928 | if (d == text) |
| 2929 | goto end; |
| 2930 | |
| 2931 | d--; |
| 2932 | dig10 |= (unsigned int)(*d << s); |
| 2933 | if (d == text) |
| 2934 | goto end; |
| 2935 | |
| 2936 | d--; |
| 2937 | dig100 |= (unsigned int)(*d << s); |
| 2938 | end: |
| 2939 | if (!s || p == stop || *p != '.') |
| 2940 | break; |
| 2941 | |
| 2942 | s -= 8; |
| 2943 | text = ++p; |
| 2944 | } |
| 2945 | |
| 2946 | dig100 -= ascii_zero; |
| 2947 | dig10 -= ascii_zero; |
| 2948 | dig1 -= ascii_zero; |
| 2949 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 2950 | } |
| 2951 | |
| 2952 | /* |
| 2953 | * Idem except the pointer to first unparsed byte is returned into <ret> which |
| 2954 | * must not be NULL. |
| 2955 | */ |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 2956 | unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret) |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2957 | { |
| 2958 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 2959 | register unsigned int dig100, dig10, dig1; |
| 2960 | int s; |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 2961 | char *p, *d; |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 2962 | |
| 2963 | dig1 = dig10 = dig100 = ascii_zero; |
| 2964 | s = 24; |
| 2965 | |
| 2966 | p = text; |
| 2967 | while (1) { |
| 2968 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 2969 | p++; |
| 2970 | continue; |
| 2971 | } |
| 2972 | |
| 2973 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 2974 | if (p == text) |
| 2975 | goto end; |
| 2976 | |
| 2977 | d = p - 1; |
| 2978 | dig1 |= (unsigned int)(*d << s); |
| 2979 | if (d == text) |
| 2980 | goto end; |
| 2981 | |
| 2982 | d--; |
| 2983 | dig10 |= (unsigned int)(*d << s); |
| 2984 | if (d == text) |
| 2985 | goto end; |
| 2986 | |
| 2987 | d--; |
| 2988 | dig100 |= (unsigned int)(*d << s); |
| 2989 | end: |
| 2990 | if (!s || p == stop || *p != '.') |
| 2991 | break; |
| 2992 | |
| 2993 | s -= 8; |
| 2994 | text = ++p; |
| 2995 | } |
| 2996 | |
| 2997 | *ret = p; |
| 2998 | dig100 -= ascii_zero; |
| 2999 | dig10 -= ascii_zero; |
| 3000 | dig1 -= ascii_zero; |
| 3001 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 3002 | } |
| 3003 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 3004 | /* Convert a fixed-length string to an IP address. Returns 0 in case of error, |
| 3005 | * or the number of chars read in case of success. Maybe this could be replaced |
| 3006 | * by one of the functions above. Also, apparently this function does not support |
| 3007 | * hosts above 255 and requires exactly 4 octets. |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 3008 | * The destination is only modified on success. |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 3009 | */ |
| 3010 | int buf2ip(const char *buf, size_t len, struct in_addr *dst) |
| 3011 | { |
| 3012 | const char *addr; |
| 3013 | int saw_digit, octets, ch; |
| 3014 | u_char tmp[4], *tp; |
| 3015 | const char *cp = buf; |
| 3016 | |
| 3017 | saw_digit = 0; |
| 3018 | octets = 0; |
| 3019 | *(tp = tmp) = 0; |
| 3020 | |
| 3021 | for (addr = buf; addr - buf < len; addr++) { |
| 3022 | unsigned char digit = (ch = *addr) - '0'; |
| 3023 | |
| 3024 | if (digit > 9 && ch != '.') |
| 3025 | break; |
| 3026 | |
| 3027 | if (digit <= 9) { |
| 3028 | u_int new = *tp * 10 + digit; |
| 3029 | |
| 3030 | if (new > 255) |
| 3031 | return 0; |
| 3032 | |
| 3033 | *tp = new; |
| 3034 | |
| 3035 | if (!saw_digit) { |
| 3036 | if (++octets > 4) |
| 3037 | return 0; |
| 3038 | saw_digit = 1; |
| 3039 | } |
| 3040 | } else if (ch == '.' && saw_digit) { |
| 3041 | if (octets == 4) |
| 3042 | return 0; |
| 3043 | |
| 3044 | *++tp = 0; |
| 3045 | saw_digit = 0; |
| 3046 | } else |
| 3047 | return 0; |
| 3048 | } |
| 3049 | |
| 3050 | if (octets < 4) |
| 3051 | return 0; |
| 3052 | |
| 3053 | memcpy(&dst->s_addr, tmp, 4); |
| 3054 | return addr - cp; |
| 3055 | } |
| 3056 | |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3057 | /* This function converts the string in <buf> of the len <len> to |
| 3058 | * struct in6_addr <dst> which must be allocated by the caller. |
| 3059 | * This function returns 1 in success case, otherwise zero. |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 3060 | * The destination is only modified on success. |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3061 | */ |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3062 | int buf2ip6(const char *buf, size_t len, struct in6_addr *dst) |
| 3063 | { |
Thierry FOURNIER | cd65991 | 2013-12-11 12:33:54 +0100 | [diff] [blame] | 3064 | char null_term_ip6[INET6_ADDRSTRLEN + 1]; |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 3065 | struct in6_addr out; |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3066 | |
Thierry FOURNIER | cd65991 | 2013-12-11 12:33:54 +0100 | [diff] [blame] | 3067 | if (len > INET6_ADDRSTRLEN) |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3068 | return 0; |
| 3069 | |
| 3070 | memcpy(null_term_ip6, buf, len); |
| 3071 | null_term_ip6[len] = '\0'; |
| 3072 | |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 3073 | if (!inet_pton(AF_INET6, null_term_ip6, &out)) |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3074 | return 0; |
| 3075 | |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 3076 | *dst = out; |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 3077 | return 1; |
| 3078 | } |
| 3079 | |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 3080 | /* To be used to quote config arg positions. Returns the short string at <ptr> |
| 3081 | * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line" |
| 3082 | * if ptr is NULL or empty. The string is locally allocated. |
| 3083 | */ |
| 3084 | const char *quote_arg(const char *ptr) |
| 3085 | { |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 3086 | static THREAD_LOCAL char val[32]; |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 3087 | int i; |
| 3088 | |
| 3089 | if (!ptr || !*ptr) |
| 3090 | return "end of line"; |
| 3091 | val[0] = '\''; |
Willy Tarreau | de2dd6b | 2013-01-24 02:14:42 +0100 | [diff] [blame] | 3092 | for (i = 1; i < sizeof(val) - 2 && *ptr; i++) |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 3093 | val[i] = *ptr++; |
| 3094 | val[i++] = '\''; |
| 3095 | val[i] = '\0'; |
| 3096 | return val; |
| 3097 | } |
| 3098 | |
Willy Tarreau | 5b18020 | 2010-07-18 10:40:48 +0200 | [diff] [blame] | 3099 | /* returns an operator among STD_OP_* for string <str> or < 0 if unknown */ |
| 3100 | int get_std_op(const char *str) |
| 3101 | { |
| 3102 | int ret = -1; |
| 3103 | |
| 3104 | if (*str == 'e' && str[1] == 'q') |
| 3105 | ret = STD_OP_EQ; |
| 3106 | else if (*str == 'n' && str[1] == 'e') |
| 3107 | ret = STD_OP_NE; |
| 3108 | else if (*str == 'l') { |
| 3109 | if (str[1] == 'e') ret = STD_OP_LE; |
| 3110 | else if (str[1] == 't') ret = STD_OP_LT; |
| 3111 | } |
| 3112 | else if (*str == 'g') { |
| 3113 | if (str[1] == 'e') ret = STD_OP_GE; |
| 3114 | else if (str[1] == 't') ret = STD_OP_GT; |
| 3115 | } |
| 3116 | |
| 3117 | if (ret == -1 || str[2] != '\0') |
| 3118 | return -1; |
| 3119 | return ret; |
| 3120 | } |
| 3121 | |
Willy Tarreau | 4c14eaa | 2010-11-24 14:01:45 +0100 | [diff] [blame] | 3122 | /* hash a 32-bit integer to another 32-bit integer */ |
| 3123 | unsigned int full_hash(unsigned int a) |
| 3124 | { |
| 3125 | return __full_hash(a); |
| 3126 | } |
| 3127 | |
Willy Tarreau | f324111 | 2019-02-26 09:56:22 +0100 | [diff] [blame] | 3128 | /* Return the bit position in mask <m> of the nth bit set of rank <r>, between |
| 3129 | * 0 and LONGBITS-1 included, starting from the left. For example ranks 0,1,2,3 |
| 3130 | * for mask 0x55 will be 6, 4, 2 and 0 respectively. This algorithm is based on |
| 3131 | * a popcount variant and is described here : |
| 3132 | * https://graphics.stanford.edu/~seander/bithacks.html |
| 3133 | */ |
| 3134 | unsigned int mask_find_rank_bit(unsigned int r, unsigned long m) |
| 3135 | { |
| 3136 | unsigned long a, b, c, d; |
| 3137 | unsigned int s; |
| 3138 | unsigned int t; |
| 3139 | |
| 3140 | a = m - ((m >> 1) & ~0UL/3); |
| 3141 | b = (a & ~0UL/5) + ((a >> 2) & ~0UL/5); |
| 3142 | c = (b + (b >> 4)) & ~0UL/0x11; |
| 3143 | d = (c + (c >> 8)) & ~0UL/0x101; |
| 3144 | |
| 3145 | r++; // make r be 1..64 |
| 3146 | |
| 3147 | t = 0; |
| 3148 | s = LONGBITS; |
| 3149 | if (s > 32) { |
Willy Tarreau | 9b6be3b | 2019-03-18 16:31:18 +0100 | [diff] [blame] | 3150 | unsigned long d2 = (d >> 16) >> 16; |
| 3151 | t = d2 + (d2 >> 16); |
Willy Tarreau | f324111 | 2019-02-26 09:56:22 +0100 | [diff] [blame] | 3152 | s -= ((t - r) & 256) >> 3; r -= (t & ((t - r) >> 8)); |
| 3153 | } |
| 3154 | |
| 3155 | t = (d >> (s - 16)) & 0xff; |
| 3156 | s -= ((t - r) & 256) >> 4; r -= (t & ((t - r) >> 8)); |
| 3157 | t = (c >> (s - 8)) & 0xf; |
| 3158 | s -= ((t - r) & 256) >> 5; r -= (t & ((t - r) >> 8)); |
| 3159 | t = (b >> (s - 4)) & 0x7; |
| 3160 | s -= ((t - r) & 256) >> 6; r -= (t & ((t - r) >> 8)); |
| 3161 | t = (a >> (s - 2)) & 0x3; |
| 3162 | s -= ((t - r) & 256) >> 7; r -= (t & ((t - r) >> 8)); |
| 3163 | t = (m >> (s - 1)) & 0x1; |
| 3164 | s -= ((t - r) & 256) >> 8; |
| 3165 | |
| 3166 | return s - 1; |
| 3167 | } |
| 3168 | |
| 3169 | /* Same as mask_find_rank_bit() above but makes use of pre-computed bitmaps |
| 3170 | * based on <m>, in <a..d>. These ones must be updated whenever <m> changes |
| 3171 | * using mask_prep_rank_map() below. |
| 3172 | */ |
| 3173 | unsigned int mask_find_rank_bit_fast(unsigned int r, unsigned long m, |
| 3174 | unsigned long a, unsigned long b, |
| 3175 | unsigned long c, unsigned long d) |
| 3176 | { |
| 3177 | unsigned int s; |
| 3178 | unsigned int t; |
| 3179 | |
| 3180 | r++; // make r be 1..64 |
| 3181 | |
| 3182 | t = 0; |
| 3183 | s = LONGBITS; |
| 3184 | if (s > 32) { |
Willy Tarreau | 9b6be3b | 2019-03-18 16:31:18 +0100 | [diff] [blame] | 3185 | unsigned long d2 = (d >> 16) >> 16; |
| 3186 | t = d2 + (d2 >> 16); |
Willy Tarreau | f324111 | 2019-02-26 09:56:22 +0100 | [diff] [blame] | 3187 | s -= ((t - r) & 256) >> 3; r -= (t & ((t - r) >> 8)); |
| 3188 | } |
| 3189 | |
| 3190 | t = (d >> (s - 16)) & 0xff; |
| 3191 | s -= ((t - r) & 256) >> 4; r -= (t & ((t - r) >> 8)); |
| 3192 | t = (c >> (s - 8)) & 0xf; |
| 3193 | s -= ((t - r) & 256) >> 5; r -= (t & ((t - r) >> 8)); |
| 3194 | t = (b >> (s - 4)) & 0x7; |
| 3195 | s -= ((t - r) & 256) >> 6; r -= (t & ((t - r) >> 8)); |
| 3196 | t = (a >> (s - 2)) & 0x3; |
| 3197 | s -= ((t - r) & 256) >> 7; r -= (t & ((t - r) >> 8)); |
| 3198 | t = (m >> (s - 1)) & 0x1; |
| 3199 | s -= ((t - r) & 256) >> 8; |
| 3200 | |
| 3201 | return s - 1; |
| 3202 | } |
| 3203 | |
| 3204 | /* Prepare the bitmaps used by the fast implementation of the find_rank_bit() |
| 3205 | * above. |
| 3206 | */ |
| 3207 | void mask_prep_rank_map(unsigned long m, |
| 3208 | unsigned long *a, unsigned long *b, |
| 3209 | unsigned long *c, unsigned long *d) |
| 3210 | { |
| 3211 | *a = m - ((m >> 1) & ~0UL/3); |
| 3212 | *b = (*a & ~0UL/5) + ((*a >> 2) & ~0UL/5); |
| 3213 | *c = (*b + (*b >> 4)) & ~0UL/0x11; |
| 3214 | *d = (*c + (*c >> 8)) & ~0UL/0x101; |
| 3215 | } |
| 3216 | |
Willy Tarreau | c7a8a3c | 2022-06-21 20:19:54 +0200 | [diff] [blame] | 3217 | /* Returns the position of one bit set in <v>, starting at position <bit>, and |
| 3218 | * searching in other halves if not found. This is intended to be used to |
| 3219 | * report the position of one bit set among several based on a counter or a |
| 3220 | * random generator while preserving a relatively good distribution so that |
| 3221 | * values made of holes in the middle do not see one of the bits around the |
| 3222 | * hole being returned much more often than the other one. It can be seen as a |
| 3223 | * disturbed ffsl() where the initial search starts at bit <bit>. The look up |
| 3224 | * is performed in O(logN) time for N bit words, yielding a bit among 64 in |
| 3225 | * about 16 cycles. Its usage differs from the rank find function in that the |
| 3226 | * bit passed doesn't need to be limited to the value's popcount, making the |
| 3227 | * function easier to use for random picking, and twice as fast. Passing value |
| 3228 | * 0 for <v> makes no sense and -1 is returned in this case. |
| 3229 | */ |
| 3230 | int one_among_mask(unsigned long v, int bit) |
| 3231 | { |
| 3232 | /* note, these masks may be produced by ~0UL/((1UL<<scale)+1) but |
| 3233 | * that's more expensive. |
| 3234 | */ |
| 3235 | static const unsigned long halves[] = { |
| 3236 | (unsigned long)0x5555555555555555ULL, |
| 3237 | (unsigned long)0x3333333333333333ULL, |
| 3238 | (unsigned long)0x0F0F0F0F0F0F0F0FULL, |
| 3239 | (unsigned long)0x00FF00FF00FF00FFULL, |
| 3240 | (unsigned long)0x0000FFFF0000FFFFULL, |
| 3241 | (unsigned long)0x00000000FFFFFFFFULL |
| 3242 | }; |
| 3243 | unsigned long halfword = ~0UL; |
| 3244 | int scope = 0; |
| 3245 | int mirror; |
| 3246 | int scale; |
| 3247 | |
| 3248 | if (!v) |
| 3249 | return -1; |
| 3250 | |
| 3251 | /* we check if the exact bit is set or if it's present in a mirror |
| 3252 | * position based on the current scale we're checking, in which case |
| 3253 | * it's returned with its current (or mirrored) value. Otherwise we'll |
| 3254 | * make sure there's at least one bit in the half we're in, and will |
| 3255 | * scale down to a smaller scope and try again, until we find the |
| 3256 | * closest bit. |
| 3257 | */ |
| 3258 | for (scale = (sizeof(long) > 4) ? 5 : 4; scale >= 0; scale--) { |
| 3259 | halfword >>= (1UL << scale); |
| 3260 | scope |= (1UL << scale); |
| 3261 | mirror = bit ^ (1UL << scale); |
| 3262 | if (v & ((1UL << bit) | (1UL << mirror))) |
| 3263 | return (v & (1UL << bit)) ? bit : mirror; |
| 3264 | |
| 3265 | if (!((v >> (bit & scope)) & halves[scale] & halfword)) |
| 3266 | bit = mirror; |
| 3267 | } |
| 3268 | return bit; |
| 3269 | } |
| 3270 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3271 | /* Return non-zero if IPv4 address is part of the network, |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3272 | * otherwise zero. Note that <addr> may not necessarily be aligned |
| 3273 | * while the two other ones must. |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3274 | */ |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3275 | 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] | 3276 | { |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3277 | struct in_addr addr_copy; |
| 3278 | |
| 3279 | memcpy(&addr_copy, addr, sizeof(addr_copy)); |
| 3280 | 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] | 3281 | } |
| 3282 | |
| 3283 | /* Return non-zero if IPv6 address is part of the network, |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3284 | * otherwise zero. Note that <addr> may not necessarily be aligned |
| 3285 | * while the two other ones must. |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3286 | */ |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3287 | 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] | 3288 | { |
| 3289 | int i; |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3290 | struct in6_addr addr_copy; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3291 | |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3292 | memcpy(&addr_copy, addr, sizeof(addr_copy)); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3293 | for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++) |
Willy Tarreau | eec1d38 | 2016-07-13 11:59:39 +0200 | [diff] [blame] | 3294 | if (((((int *)&addr_copy)[i] & ((int *)mask)[i])) != |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3295 | (((int *)net)[i] & ((int *)mask)[i])) |
| 3296 | return 0; |
| 3297 | return 1; |
| 3298 | } |
| 3299 | |
| 3300 | /* RFC 4291 prefix */ |
| 3301 | const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00, |
| 3302 | 0x00, 0x00, 0x00, 0x00, |
| 3303 | 0x00, 0x00, 0xFF, 0xFF }; |
| 3304 | |
Joseph Herlant | 32b8327 | 2018-11-15 11:58:28 -0800 | [diff] [blame] | 3305 | /* Map IPv4 address on IPv6 address, as specified in RFC 3513. |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 3306 | * Input and output may overlap. |
| 3307 | */ |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3308 | void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr) |
| 3309 | { |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 3310 | struct in_addr tmp_addr; |
| 3311 | |
| 3312 | tmp_addr.s_addr = sin_addr->s_addr; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3313 | memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)); |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 3314 | memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3315 | } |
| 3316 | |
Joseph Herlant | 32b8327 | 2018-11-15 11:58:28 -0800 | [diff] [blame] | 3317 | /* Map IPv6 address on IPv4 address, as specified in RFC 3513. |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 3318 | * Return true if conversion is possible and false otherwise. |
| 3319 | */ |
| 3320 | int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr) |
| 3321 | { |
| 3322 | if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) { |
| 3323 | memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]), |
| 3324 | sizeof(struct in_addr)); |
| 3325 | return 1; |
| 3326 | } |
| 3327 | |
| 3328 | return 0; |
| 3329 | } |
| 3330 | |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3331 | /* compare two struct sockaddr_storage, including port if <check_port> is true, |
| 3332 | * and return: |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3333 | * 0 (true) if the addr is the same in both |
| 3334 | * 1 (false) if the addr is not the same in both |
| 3335 | * -1 (unable) if one of the addr is not AF_INET* |
| 3336 | */ |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3337 | int ipcmp(struct sockaddr_storage *ss1, struct sockaddr_storage *ss2, int check_port) |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3338 | { |
| 3339 | if ((ss1->ss_family != AF_INET) && (ss1->ss_family != AF_INET6)) |
| 3340 | return -1; |
| 3341 | |
| 3342 | if ((ss2->ss_family != AF_INET) && (ss2->ss_family != AF_INET6)) |
| 3343 | return -1; |
| 3344 | |
| 3345 | if (ss1->ss_family != ss2->ss_family) |
| 3346 | return 1; |
| 3347 | |
| 3348 | switch (ss1->ss_family) { |
| 3349 | case AF_INET: |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3350 | return (memcmp(&((struct sockaddr_in *)ss1)->sin_addr, |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3351 | &((struct sockaddr_in *)ss2)->sin_addr, |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3352 | sizeof(struct in_addr)) != 0) || |
| 3353 | (check_port && get_net_port(ss1) != get_net_port(ss2)); |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3354 | case AF_INET6: |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3355 | return (memcmp(&((struct sockaddr_in6 *)ss1)->sin6_addr, |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3356 | &((struct sockaddr_in6 *)ss2)->sin6_addr, |
Amaury Denoyelle | 21e611d | 2022-12-01 17:46:45 +0100 | [diff] [blame] | 3357 | sizeof(struct in6_addr)) != 0) || |
| 3358 | (check_port && get_net_port(ss1) != get_net_port(ss2)); |
Baptiste Assmann | 08b24cf | 2016-01-23 23:39:12 +0100 | [diff] [blame] | 3359 | } |
| 3360 | |
| 3361 | return 1; |
| 3362 | } |
| 3363 | |
Christopher Faulet | 9553de7 | 2021-02-26 09:12:50 +0100 | [diff] [blame] | 3364 | /* compare a struct sockaddr_storage to a struct net_addr and return : |
| 3365 | * 0 (true) if <addr> is matching <net> |
| 3366 | * 1 (false) if <addr> is not matching <net> |
| 3367 | * -1 (unable) if <addr> or <net> is not AF_INET* |
| 3368 | */ |
| 3369 | int ipcmp2net(const struct sockaddr_storage *addr, const struct net_addr *net) |
| 3370 | { |
| 3371 | if ((addr->ss_family != AF_INET) && (addr->ss_family != AF_INET6)) |
| 3372 | return -1; |
| 3373 | |
| 3374 | if ((net->family != AF_INET) && (net->family != AF_INET6)) |
| 3375 | return -1; |
| 3376 | |
| 3377 | if (addr->ss_family != net->family) |
| 3378 | return 1; |
| 3379 | |
| 3380 | if (addr->ss_family == AF_INET && |
| 3381 | (((struct sockaddr_in *)addr)->sin_addr.s_addr & net->addr.v4.mask.s_addr) == net->addr.v4.ip.s_addr) |
| 3382 | return 0; |
| 3383 | else { |
| 3384 | const struct in6_addr *addr6 = &(((const struct sockaddr_in6*)addr)->sin6_addr); |
| 3385 | const struct in6_addr *nip6 = &net->addr.v6.ip; |
| 3386 | const struct in6_addr *nmask6 = &net->addr.v6.mask; |
| 3387 | |
| 3388 | if ((read_u32(&addr6->s6_addr[0]) & read_u32(&nmask6->s6_addr[0])) == read_u32(&nip6->s6_addr[0]) && |
| 3389 | (read_u32(&addr6->s6_addr[4]) & read_u32(&nmask6->s6_addr[4])) == read_u32(&nip6->s6_addr[4]) && |
| 3390 | (read_u32(&addr6->s6_addr[8]) & read_u32(&nmask6->s6_addr[8])) == read_u32(&nip6->s6_addr[8]) && |
| 3391 | (read_u32(&addr6->s6_addr[12]) & read_u32(&nmask6->s6_addr[12])) == read_u32(&nip6->s6_addr[12])) |
| 3392 | return 0; |
| 3393 | } |
| 3394 | |
| 3395 | return 1; |
| 3396 | } |
| 3397 | |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3398 | /* copy IP address from <source> into <dest> |
Willy Tarreau | dc3a9e8 | 2016-11-04 18:47:01 +0100 | [diff] [blame] | 3399 | * The caller must allocate and clear <dest> before calling. |
| 3400 | * The source must be in either AF_INET or AF_INET6 family, or the destination |
| 3401 | * address will be undefined. If the destination address used to hold a port, |
| 3402 | * it is preserved, so that this function can be used to switch to another |
| 3403 | * address family with no risk. Returns a pointer to the destination. |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3404 | */ |
| 3405 | struct sockaddr_storage *ipcpy(struct sockaddr_storage *source, struct sockaddr_storage *dest) |
| 3406 | { |
Willy Tarreau | dc3a9e8 | 2016-11-04 18:47:01 +0100 | [diff] [blame] | 3407 | int prev_port; |
| 3408 | |
| 3409 | prev_port = get_net_port(dest); |
| 3410 | memset(dest, 0, sizeof(*dest)); |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3411 | dest->ss_family = source->ss_family; |
| 3412 | |
| 3413 | /* copy new addr and apply it */ |
| 3414 | switch (source->ss_family) { |
| 3415 | case AF_INET: |
| 3416 | ((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] | 3417 | ((struct sockaddr_in *)dest)->sin_port = prev_port; |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3418 | break; |
| 3419 | case AF_INET6: |
| 3420 | 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] | 3421 | ((struct sockaddr_in6 *)dest)->sin6_port = prev_port; |
Baptiste Assmann | 08396c8 | 2016-01-31 00:27:17 +0100 | [diff] [blame] | 3422 | break; |
| 3423 | } |
| 3424 | |
| 3425 | return dest; |
| 3426 | } |
| 3427 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3428 | char *human_time(int t, short hz_div) { |
| 3429 | static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s" |
| 3430 | char *p = rv; |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3431 | char *end = rv + sizeof(rv); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3432 | int cnt=2; // print two numbers |
| 3433 | |
| 3434 | if (unlikely(t < 0 || hz_div <= 0)) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3435 | snprintf(p, end - p, "?"); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3436 | return rv; |
| 3437 | } |
| 3438 | |
| 3439 | if (unlikely(hz_div > 1)) |
| 3440 | t /= hz_div; |
| 3441 | |
| 3442 | if (t >= DAY) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3443 | p += snprintf(p, end - p, "%dd", t / DAY); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3444 | cnt--; |
| 3445 | } |
| 3446 | |
| 3447 | if (cnt && t % DAY / HOUR) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3448 | p += snprintf(p, end - p, "%dh", t % DAY / HOUR); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3449 | cnt--; |
| 3450 | } |
| 3451 | |
| 3452 | if (cnt && t % HOUR / MINUTE) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3453 | p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3454 | cnt--; |
| 3455 | } |
| 3456 | |
| 3457 | if ((cnt && t % MINUTE) || !t) // also display '0s' |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 3458 | p += snprintf(p, end - p, "%ds", t % MINUTE / SEC); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3459 | |
| 3460 | return rv; |
| 3461 | } |
| 3462 | |
| 3463 | const char *monthname[12] = { |
| 3464 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 3465 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 3466 | }; |
| 3467 | |
| 3468 | /* date2str_log: write a date in the format : |
| 3469 | * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d", |
| 3470 | * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 3471 | * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000); |
| 3472 | * |
| 3473 | * without using sprintf. return a pointer to the last char written (\0) or |
| 3474 | * NULL if there isn't enough space. |
| 3475 | */ |
Willy Tarreau | f16cb41 | 2018-09-04 19:08:48 +0200 | [diff] [blame] | 3476 | 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] | 3477 | { |
| 3478 | |
| 3479 | if (size < 25) /* the size is fixed: 24 chars + \0 */ |
| 3480 | return NULL; |
| 3481 | |
| 3482 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3483 | if (!dst) |
| 3484 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3485 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3486 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3487 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 3488 | dst += 3; |
| 3489 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3490 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3491 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3492 | if (!dst) |
| 3493 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3494 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3495 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3496 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3497 | if (!dst) |
| 3498 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3499 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3500 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3501 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3502 | if (!dst) |
| 3503 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3504 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3505 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3506 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3507 | if (!dst) |
| 3508 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3509 | *dst++ = '.'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3510 | |
Willy Tarreau | 7d9421d | 2020-02-29 09:08:02 +0100 | [diff] [blame] | 3511 | 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] | 3512 | if (!dst) |
| 3513 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3514 | *dst = '\0'; |
| 3515 | |
| 3516 | return dst; |
| 3517 | } |
| 3518 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3519 | /* Base year used to compute leap years */ |
| 3520 | #define TM_YEAR_BASE 1900 |
| 3521 | |
| 3522 | /* Return the difference in seconds between two times (leap seconds are ignored). |
| 3523 | * Retrieved from glibc 2.18 source code. |
| 3524 | */ |
| 3525 | static int my_tm_diff(const struct tm *a, const struct tm *b) |
| 3526 | { |
| 3527 | /* Compute intervening leap days correctly even if year is negative. |
| 3528 | * Take care to avoid int overflow in leap day calculations, |
| 3529 | * but it's OK to assume that A and B are close to each other. |
| 3530 | */ |
| 3531 | int a4 = (a->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (a->tm_year & 3); |
| 3532 | int b4 = (b->tm_year >> 2) + (TM_YEAR_BASE >> 2) - ! (b->tm_year & 3); |
| 3533 | int a100 = a4 / 25 - (a4 % 25 < 0); |
| 3534 | int b100 = b4 / 25 - (b4 % 25 < 0); |
| 3535 | int a400 = a100 >> 2; |
| 3536 | int b400 = b100 >> 2; |
| 3537 | int intervening_leap_days = (a4 - b4) - (a100 - b100) + (a400 - b400); |
| 3538 | int years = a->tm_year - b->tm_year; |
| 3539 | int days = (365 * years + intervening_leap_days |
| 3540 | + (a->tm_yday - b->tm_yday)); |
| 3541 | return (60 * (60 * (24 * days + (a->tm_hour - b->tm_hour)) |
| 3542 | + (a->tm_min - b->tm_min)) |
| 3543 | + (a->tm_sec - b->tm_sec)); |
| 3544 | } |
| 3545 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3546 | /* Return the GMT offset for a specific local time. |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3547 | * Both t and tm must represent the same time. |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3548 | * The string returned has the same format as returned by strftime(... "%z", tm). |
| 3549 | * Offsets are kept in an internal cache for better performances. |
| 3550 | */ |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3551 | const char *get_gmt_offset(time_t t, struct tm *tm) |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3552 | { |
| 3553 | /* Cache offsets from GMT (depending on whether DST is active or not) */ |
Christopher Faulet | 1bc04c7 | 2017-10-29 20:14:08 +0100 | [diff] [blame] | 3554 | static THREAD_LOCAL char gmt_offsets[2][5+1] = { "", "" }; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3555 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3556 | char *gmt_offset; |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3557 | struct tm tm_gmt; |
| 3558 | int diff; |
| 3559 | int isdst = tm->tm_isdst; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3560 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3561 | /* Pretend DST not active if its status is unknown */ |
| 3562 | if (isdst < 0) |
| 3563 | isdst = 0; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3564 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3565 | /* Fetch the offset and initialize it if needed */ |
| 3566 | gmt_offset = gmt_offsets[isdst & 0x01]; |
| 3567 | if (unlikely(!*gmt_offset)) { |
| 3568 | get_gmtime(t, &tm_gmt); |
| 3569 | diff = my_tm_diff(tm, &tm_gmt); |
| 3570 | if (diff < 0) { |
| 3571 | diff = -diff; |
| 3572 | *gmt_offset = '-'; |
| 3573 | } else { |
| 3574 | *gmt_offset = '+'; |
| 3575 | } |
Willy Tarreau | e112c8a | 2019-10-29 10:16:11 +0100 | [diff] [blame] | 3576 | diff %= 86400U; |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3577 | diff /= 60; /* Convert to minutes */ |
| 3578 | snprintf(gmt_offset+1, 4+1, "%02d%02d", diff/60, diff%60); |
| 3579 | } |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3580 | |
Willy Tarreau | e112c8a | 2019-10-29 10:16:11 +0100 | [diff] [blame] | 3581 | return gmt_offset; |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3582 | } |
| 3583 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3584 | /* gmt2str_log: write a date in the format : |
| 3585 | * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf |
| 3586 | * return a pointer to the last char written (\0) or |
| 3587 | * NULL if there isn't enough space. |
| 3588 | */ |
| 3589 | char *gmt2str_log(char *dst, struct tm *tm, size_t size) |
| 3590 | { |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3591 | if (size < 27) /* the size is fixed: 26 chars + \0 */ |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3592 | return NULL; |
| 3593 | |
| 3594 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3595 | if (!dst) |
| 3596 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3597 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3598 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3599 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 3600 | dst += 3; |
| 3601 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3602 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3603 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3604 | if (!dst) |
| 3605 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3606 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3607 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3608 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3609 | if (!dst) |
| 3610 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3611 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3612 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3613 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3614 | if (!dst) |
| 3615 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3616 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3617 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3618 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3619 | if (!dst) |
| 3620 | return NULL; |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 3621 | *dst++ = ' '; |
| 3622 | *dst++ = '+'; |
| 3623 | *dst++ = '0'; |
| 3624 | *dst++ = '0'; |
| 3625 | *dst++ = '0'; |
| 3626 | *dst++ = '0'; |
| 3627 | *dst = '\0'; |
| 3628 | |
| 3629 | return dst; |
| 3630 | } |
| 3631 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3632 | /* localdate2str_log: write a date in the format : |
| 3633 | * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3634 | * Both t and tm must represent the same time. |
| 3635 | * return a pointer to the last char written (\0) or |
| 3636 | * NULL if there isn't enough space. |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3637 | */ |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3638 | 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] | 3639 | { |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3640 | const char *gmt_offset; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3641 | if (size < 27) /* the size is fixed: 26 chars + \0 */ |
| 3642 | return NULL; |
| 3643 | |
Benoit GARNIER | e2e5bde | 2016-03-27 03:04:16 +0200 | [diff] [blame] | 3644 | gmt_offset = get_gmt_offset(t, tm); |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3645 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3646 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3647 | if (!dst) |
| 3648 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3649 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3650 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3651 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 3652 | dst += 3; |
| 3653 | *dst++ = '/'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3654 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3655 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3656 | if (!dst) |
| 3657 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3658 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3659 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3660 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3661 | if (!dst) |
| 3662 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3663 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3664 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3665 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3666 | if (!dst) |
| 3667 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3668 | *dst++ = ':'; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3669 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3670 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3671 | if (!dst) |
| 3672 | return NULL; |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3673 | *dst++ = ' '; |
Willy Tarreau | 4eee38a | 2019-02-12 11:26:29 +0100 | [diff] [blame] | 3674 | |
Benoit GARNIER | b413c2a | 2016-03-27 11:08:03 +0200 | [diff] [blame] | 3675 | memcpy(dst, gmt_offset, 5); // Offset from local time to GMT |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 3676 | dst += 5; |
| 3677 | *dst = '\0'; |
| 3678 | |
| 3679 | return dst; |
| 3680 | } |
| 3681 | |
Willy Tarreau | cb1949b | 2017-07-19 19:05:29 +0200 | [diff] [blame] | 3682 | /* Returns the number of seconds since 01/01/1970 0:0:0 GMT for GMT date <tm>. |
| 3683 | * It is meant as a portable replacement for timegm() for use with valid inputs. |
| 3684 | * Returns undefined results for invalid dates (eg: months out of range 0..11). |
| 3685 | */ |
| 3686 | time_t my_timegm(const struct tm *tm) |
| 3687 | { |
| 3688 | /* Each month has 28, 29, 30 or 31 days, or 28+N. The date in the year |
| 3689 | * is thus (current month - 1)*28 + cumulated_N[month] to count the |
| 3690 | * sum of the extra N days for elapsed months. The sum of all these N |
| 3691 | * days doesn't exceed 30 for a complete year (366-12*28) so it fits |
| 3692 | * in a 5-bit word. This means that with 60 bits we can represent a |
| 3693 | * matrix of all these values at once, which is fast and efficient to |
| 3694 | * access. The extra February day for leap years is not counted here. |
| 3695 | * |
| 3696 | * Jan : none = 0 (0) |
| 3697 | * Feb : Jan = 3 (3) |
| 3698 | * Mar : Jan..Feb = 3 (3 + 0) |
| 3699 | * Apr : Jan..Mar = 6 (3 + 0 + 3) |
| 3700 | * May : Jan..Apr = 8 (3 + 0 + 3 + 2) |
| 3701 | * Jun : Jan..May = 11 (3 + 0 + 3 + 2 + 3) |
| 3702 | * Jul : Jan..Jun = 13 (3 + 0 + 3 + 2 + 3 + 2) |
| 3703 | * Aug : Jan..Jul = 16 (3 + 0 + 3 + 2 + 3 + 2 + 3) |
| 3704 | * Sep : Jan..Aug = 19 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3) |
| 3705 | * Oct : Jan..Sep = 21 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3 + 2) |
| 3706 | * Nov : Jan..Oct = 24 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3 + 2 + 3) |
| 3707 | * Dec : Jan..Nov = 26 (3 + 0 + 3 + 2 + 3 + 2 + 3 + 3 + 2 + 3 + 2) |
| 3708 | */ |
| 3709 | uint64_t extra = |
| 3710 | ( 0ULL << 0*5) + ( 3ULL << 1*5) + ( 3ULL << 2*5) + /* Jan, Feb, Mar, */ |
| 3711 | ( 6ULL << 3*5) + ( 8ULL << 4*5) + (11ULL << 5*5) + /* Apr, May, Jun, */ |
| 3712 | (13ULL << 6*5) + (16ULL << 7*5) + (19ULL << 8*5) + /* Jul, Aug, Sep, */ |
| 3713 | (21ULL << 9*5) + (24ULL << 10*5) + (26ULL << 11*5); /* Oct, Nov, Dec, */ |
| 3714 | |
| 3715 | unsigned int y = tm->tm_year + 1900; |
| 3716 | unsigned int m = tm->tm_mon; |
| 3717 | unsigned long days = 0; |
| 3718 | |
| 3719 | /* days since 1/1/1970 for full years */ |
| 3720 | days += days_since_zero(y) - days_since_zero(1970); |
| 3721 | |
| 3722 | /* days for full months in the current year */ |
| 3723 | days += 28 * m + ((extra >> (m * 5)) & 0x1f); |
| 3724 | |
| 3725 | /* count + 1 after March for leap years. A leap year is a year multiple |
| 3726 | * of 4, unless it's multiple of 100 without being multiple of 400. 2000 |
| 3727 | * is leap, 1900 isn't, 1904 is. |
| 3728 | */ |
| 3729 | if ((m > 1) && !(y & 3) && ((y % 100) || !(y % 400))) |
| 3730 | days++; |
| 3731 | |
| 3732 | days += tm->tm_mday - 1; |
| 3733 | return days * 86400ULL + tm->tm_hour * 3600 + tm->tm_min * 60 + tm->tm_sec; |
| 3734 | } |
| 3735 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3736 | /* This function check a char. It returns true and updates |
| 3737 | * <date> and <len> pointer to the new position if the |
| 3738 | * character is found. |
| 3739 | */ |
| 3740 | static inline int parse_expect_char(const char **date, int *len, char c) |
| 3741 | { |
| 3742 | if (*len < 1 || **date != c) |
| 3743 | return 0; |
| 3744 | (*len)--; |
| 3745 | (*date)++; |
| 3746 | return 1; |
| 3747 | } |
| 3748 | |
| 3749 | /* This function expects a string <str> of len <l>. It return true and updates. |
| 3750 | * <date> and <len> if the string matches, otherwise, it returns false. |
| 3751 | */ |
| 3752 | static inline int parse_strcmp(const char **date, int *len, char *str, int l) |
| 3753 | { |
| 3754 | if (*len < l || strncmp(*date, str, l) != 0) |
| 3755 | return 0; |
| 3756 | (*len) -= l; |
| 3757 | (*date) += l; |
| 3758 | return 1; |
| 3759 | } |
| 3760 | |
| 3761 | /* This macro converts 3 chars name in integer. */ |
| 3762 | #define STR2I3(__a, __b, __c) ((__a) * 65536 + (__b) * 256 + (__c)) |
| 3763 | |
| 3764 | /* day-name = %x4D.6F.6E ; "Mon", case-sensitive |
| 3765 | * / %x54.75.65 ; "Tue", case-sensitive |
| 3766 | * / %x57.65.64 ; "Wed", case-sensitive |
| 3767 | * / %x54.68.75 ; "Thu", case-sensitive |
| 3768 | * / %x46.72.69 ; "Fri", case-sensitive |
| 3769 | * / %x53.61.74 ; "Sat", case-sensitive |
| 3770 | * / %x53.75.6E ; "Sun", case-sensitive |
| 3771 | * |
| 3772 | * This array must be alphabetically sorted |
| 3773 | */ |
| 3774 | static inline int parse_http_dayname(const char **date, int *len, struct tm *tm) |
| 3775 | { |
| 3776 | if (*len < 3) |
| 3777 | return 0; |
| 3778 | switch (STR2I3((*date)[0], (*date)[1], (*date)[2])) { |
| 3779 | case STR2I3('M','o','n'): tm->tm_wday = 1; break; |
| 3780 | case STR2I3('T','u','e'): tm->tm_wday = 2; break; |
| 3781 | case STR2I3('W','e','d'): tm->tm_wday = 3; break; |
| 3782 | case STR2I3('T','h','u'): tm->tm_wday = 4; break; |
| 3783 | case STR2I3('F','r','i'): tm->tm_wday = 5; break; |
| 3784 | case STR2I3('S','a','t'): tm->tm_wday = 6; break; |
| 3785 | case STR2I3('S','u','n'): tm->tm_wday = 7; break; |
| 3786 | default: return 0; |
| 3787 | } |
| 3788 | *len -= 3; |
| 3789 | *date += 3; |
| 3790 | return 1; |
| 3791 | } |
| 3792 | |
| 3793 | /* month = %x4A.61.6E ; "Jan", case-sensitive |
| 3794 | * / %x46.65.62 ; "Feb", case-sensitive |
| 3795 | * / %x4D.61.72 ; "Mar", case-sensitive |
| 3796 | * / %x41.70.72 ; "Apr", case-sensitive |
| 3797 | * / %x4D.61.79 ; "May", case-sensitive |
| 3798 | * / %x4A.75.6E ; "Jun", case-sensitive |
| 3799 | * / %x4A.75.6C ; "Jul", case-sensitive |
| 3800 | * / %x41.75.67 ; "Aug", case-sensitive |
| 3801 | * / %x53.65.70 ; "Sep", case-sensitive |
| 3802 | * / %x4F.63.74 ; "Oct", case-sensitive |
| 3803 | * / %x4E.6F.76 ; "Nov", case-sensitive |
| 3804 | * / %x44.65.63 ; "Dec", case-sensitive |
| 3805 | * |
| 3806 | * This array must be alphabetically sorted |
| 3807 | */ |
| 3808 | static inline int parse_http_monthname(const char **date, int *len, struct tm *tm) |
| 3809 | { |
| 3810 | if (*len < 3) |
| 3811 | return 0; |
| 3812 | switch (STR2I3((*date)[0], (*date)[1], (*date)[2])) { |
| 3813 | case STR2I3('J','a','n'): tm->tm_mon = 0; break; |
| 3814 | case STR2I3('F','e','b'): tm->tm_mon = 1; break; |
| 3815 | case STR2I3('M','a','r'): tm->tm_mon = 2; break; |
| 3816 | case STR2I3('A','p','r'): tm->tm_mon = 3; break; |
| 3817 | case STR2I3('M','a','y'): tm->tm_mon = 4; break; |
| 3818 | case STR2I3('J','u','n'): tm->tm_mon = 5; break; |
| 3819 | case STR2I3('J','u','l'): tm->tm_mon = 6; break; |
| 3820 | case STR2I3('A','u','g'): tm->tm_mon = 7; break; |
| 3821 | case STR2I3('S','e','p'): tm->tm_mon = 8; break; |
| 3822 | case STR2I3('O','c','t'): tm->tm_mon = 9; break; |
| 3823 | case STR2I3('N','o','v'): tm->tm_mon = 10; break; |
| 3824 | case STR2I3('D','e','c'): tm->tm_mon = 11; break; |
| 3825 | default: return 0; |
| 3826 | } |
| 3827 | *len -= 3; |
| 3828 | *date += 3; |
| 3829 | return 1; |
| 3830 | } |
| 3831 | |
| 3832 | /* day-name-l = %x4D.6F.6E.64.61.79 ; "Monday", case-sensitive |
| 3833 | * / %x54.75.65.73.64.61.79 ; "Tuesday", case-sensitive |
| 3834 | * / %x57.65.64.6E.65.73.64.61.79 ; "Wednesday", case-sensitive |
| 3835 | * / %x54.68.75.72.73.64.61.79 ; "Thursday", case-sensitive |
| 3836 | * / %x46.72.69.64.61.79 ; "Friday", case-sensitive |
| 3837 | * / %x53.61.74.75.72.64.61.79 ; "Saturday", case-sensitive |
| 3838 | * / %x53.75.6E.64.61.79 ; "Sunday", case-sensitive |
| 3839 | * |
| 3840 | * This array must be alphabetically sorted |
| 3841 | */ |
| 3842 | static inline int parse_http_ldayname(const char **date, int *len, struct tm *tm) |
| 3843 | { |
| 3844 | if (*len < 6) /* Minimum length. */ |
| 3845 | return 0; |
| 3846 | switch (STR2I3((*date)[0], (*date)[1], (*date)[2])) { |
| 3847 | case STR2I3('M','o','n'): |
| 3848 | RET0_UNLESS(parse_strcmp(date, len, "Monday", 6)); |
| 3849 | tm->tm_wday = 1; |
| 3850 | return 1; |
| 3851 | case STR2I3('T','u','e'): |
| 3852 | RET0_UNLESS(parse_strcmp(date, len, "Tuesday", 7)); |
| 3853 | tm->tm_wday = 2; |
| 3854 | return 1; |
| 3855 | case STR2I3('W','e','d'): |
| 3856 | RET0_UNLESS(parse_strcmp(date, len, "Wednesday", 9)); |
| 3857 | tm->tm_wday = 3; |
| 3858 | return 1; |
| 3859 | case STR2I3('T','h','u'): |
| 3860 | RET0_UNLESS(parse_strcmp(date, len, "Thursday", 8)); |
| 3861 | tm->tm_wday = 4; |
| 3862 | return 1; |
| 3863 | case STR2I3('F','r','i'): |
| 3864 | RET0_UNLESS(parse_strcmp(date, len, "Friday", 6)); |
| 3865 | tm->tm_wday = 5; |
| 3866 | return 1; |
| 3867 | case STR2I3('S','a','t'): |
| 3868 | RET0_UNLESS(parse_strcmp(date, len, "Saturday", 8)); |
| 3869 | tm->tm_wday = 6; |
| 3870 | return 1; |
| 3871 | case STR2I3('S','u','n'): |
| 3872 | RET0_UNLESS(parse_strcmp(date, len, "Sunday", 6)); |
| 3873 | tm->tm_wday = 7; |
| 3874 | return 1; |
| 3875 | } |
| 3876 | return 0; |
| 3877 | } |
| 3878 | |
| 3879 | /* This function parses exactly 1 digit and returns the numeric value in "digit". */ |
| 3880 | static inline int parse_digit(const char **date, int *len, int *digit) |
| 3881 | { |
| 3882 | if (*len < 1 || **date < '0' || **date > '9') |
| 3883 | return 0; |
| 3884 | *digit = (**date - '0'); |
| 3885 | (*date)++; |
| 3886 | (*len)--; |
| 3887 | return 1; |
| 3888 | } |
| 3889 | |
| 3890 | /* This function parses exactly 2 digits and returns the numeric value in "digit". */ |
| 3891 | static inline int parse_2digit(const char **date, int *len, int *digit) |
| 3892 | { |
| 3893 | int value; |
| 3894 | |
| 3895 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3896 | (*digit) = value * 10; |
| 3897 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3898 | (*digit) += value; |
| 3899 | |
| 3900 | return 1; |
| 3901 | } |
| 3902 | |
| 3903 | /* This function parses exactly 4 digits and returns the numeric value in "digit". */ |
| 3904 | static inline int parse_4digit(const char **date, int *len, int *digit) |
| 3905 | { |
| 3906 | int value; |
| 3907 | |
| 3908 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3909 | (*digit) = value * 1000; |
| 3910 | |
| 3911 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3912 | (*digit) += value * 100; |
| 3913 | |
| 3914 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3915 | (*digit) += value * 10; |
| 3916 | |
| 3917 | RET0_UNLESS(parse_digit(date, len, &value)); |
| 3918 | (*digit) += value; |
| 3919 | |
| 3920 | return 1; |
| 3921 | } |
| 3922 | |
| 3923 | /* time-of-day = hour ":" minute ":" second |
| 3924 | * ; 00:00:00 - 23:59:60 (leap second) |
| 3925 | * |
| 3926 | * hour = 2DIGIT |
| 3927 | * minute = 2DIGIT |
| 3928 | * second = 2DIGIT |
| 3929 | */ |
| 3930 | static inline int parse_http_time(const char **date, int *len, struct tm *tm) |
| 3931 | { |
| 3932 | RET0_UNLESS(parse_2digit(date, len, &tm->tm_hour)); /* hour 2DIGIT */ |
| 3933 | RET0_UNLESS(parse_expect_char(date, len, ':')); /* expect ":" */ |
| 3934 | RET0_UNLESS(parse_2digit(date, len, &tm->tm_min)); /* min 2DIGIT */ |
| 3935 | RET0_UNLESS(parse_expect_char(date, len, ':')); /* expect ":" */ |
| 3936 | RET0_UNLESS(parse_2digit(date, len, &tm->tm_sec)); /* sec 2DIGIT */ |
| 3937 | return 1; |
| 3938 | } |
| 3939 | |
| 3940 | /* From RFC7231 |
| 3941 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 3942 | * |
| 3943 | * IMF-fixdate = day-name "," SP date1 SP time-of-day SP GMT |
| 3944 | * ; fixed length/zone/capitalization subset of the format |
| 3945 | * ; see Section 3.3 of [RFC5322] |
| 3946 | * |
| 3947 | * |
| 3948 | * date1 = day SP month SP year |
| 3949 | * ; e.g., 02 Jun 1982 |
| 3950 | * |
| 3951 | * day = 2DIGIT |
| 3952 | * year = 4DIGIT |
| 3953 | * |
| 3954 | * GMT = %x47.4D.54 ; "GMT", case-sensitive |
| 3955 | * |
| 3956 | * time-of-day = hour ":" minute ":" second |
| 3957 | * ; 00:00:00 - 23:59:60 (leap second) |
| 3958 | * |
| 3959 | * hour = 2DIGIT |
| 3960 | * minute = 2DIGIT |
| 3961 | * second = 2DIGIT |
| 3962 | * |
| 3963 | * DIGIT = decimal 0-9 |
| 3964 | */ |
| 3965 | int parse_imf_date(const char *date, int len, struct tm *tm) |
| 3966 | { |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 3967 | /* tm_gmtoff, if present, ought to be zero'ed */ |
| 3968 | memset(tm, 0, sizeof(*tm)); |
| 3969 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3970 | RET0_UNLESS(parse_http_dayname(&date, &len, tm)); /* day-name */ |
| 3971 | RET0_UNLESS(parse_expect_char(&date, &len, ',')); /* expect "," */ |
| 3972 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3973 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_mday)); /* day 2DIGIT */ |
| 3974 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3975 | RET0_UNLESS(parse_http_monthname(&date, &len, tm)); /* Month */ |
| 3976 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3977 | RET0_UNLESS(parse_4digit(&date, &len, &tm->tm_year)); /* year = 4DIGIT */ |
| 3978 | tm->tm_year -= 1900; |
| 3979 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3980 | RET0_UNLESS(parse_http_time(&date, &len, tm)); /* Parse time. */ |
| 3981 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 3982 | RET0_UNLESS(parse_strcmp(&date, &len, "GMT", 3)); /* GMT = %x47.4D.54 ; "GMT", case-sensitive */ |
| 3983 | tm->tm_isdst = -1; |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 3984 | return 1; |
| 3985 | } |
| 3986 | |
| 3987 | /* From RFC7231 |
| 3988 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 3989 | * |
| 3990 | * rfc850-date = day-name-l "," SP date2 SP time-of-day SP GMT |
| 3991 | * date2 = day "-" month "-" 2DIGIT |
| 3992 | * ; e.g., 02-Jun-82 |
| 3993 | * |
| 3994 | * day = 2DIGIT |
| 3995 | */ |
| 3996 | int parse_rfc850_date(const char *date, int len, struct tm *tm) |
| 3997 | { |
| 3998 | int year; |
| 3999 | |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 4000 | /* tm_gmtoff, if present, ought to be zero'ed */ |
| 4001 | memset(tm, 0, sizeof(*tm)); |
| 4002 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 4003 | RET0_UNLESS(parse_http_ldayname(&date, &len, tm)); /* Read the day name */ |
| 4004 | RET0_UNLESS(parse_expect_char(&date, &len, ',')); /* expect "," */ |
| 4005 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4006 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_mday)); /* day 2DIGIT */ |
| 4007 | RET0_UNLESS(parse_expect_char(&date, &len, '-')); /* expect "-" */ |
| 4008 | RET0_UNLESS(parse_http_monthname(&date, &len, tm)); /* Month */ |
| 4009 | RET0_UNLESS(parse_expect_char(&date, &len, '-')); /* expect "-" */ |
| 4010 | |
| 4011 | /* year = 2DIGIT |
| 4012 | * |
| 4013 | * Recipients of a timestamp value in rfc850-(*date) format, which uses a |
| 4014 | * two-digit year, MUST interpret a timestamp that appears to be more |
| 4015 | * than 50 years in the future as representing the most recent year in |
| 4016 | * the past that had the same last two digits. |
| 4017 | */ |
| 4018 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_year)); |
| 4019 | |
| 4020 | /* expect SP */ |
| 4021 | if (!parse_expect_char(&date, &len, ' ')) { |
| 4022 | /* Maybe we have the date with 4 digits. */ |
| 4023 | RET0_UNLESS(parse_2digit(&date, &len, &year)); |
| 4024 | tm->tm_year = (tm->tm_year * 100 + year) - 1900; |
| 4025 | /* expect SP */ |
| 4026 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); |
| 4027 | } else { |
| 4028 | /* I fix 60 as pivot: >60: +1900, <60: +2000. Note that the |
| 4029 | * tm_year is the number of year since 1900, so for +1900, we |
| 4030 | * do nothing, and for +2000, we add 100. |
| 4031 | */ |
| 4032 | if (tm->tm_year <= 60) |
| 4033 | tm->tm_year += 100; |
| 4034 | } |
| 4035 | |
| 4036 | RET0_UNLESS(parse_http_time(&date, &len, tm)); /* Parse time. */ |
| 4037 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4038 | RET0_UNLESS(parse_strcmp(&date, &len, "GMT", 3)); /* GMT = %x47.4D.54 ; "GMT", case-sensitive */ |
| 4039 | tm->tm_isdst = -1; |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 4040 | |
| 4041 | return 1; |
| 4042 | } |
| 4043 | |
| 4044 | /* From RFC7231 |
| 4045 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 4046 | * |
| 4047 | * asctime-date = day-name SP date3 SP time-of-day SP year |
| 4048 | * date3 = month SP ( 2DIGIT / ( SP 1DIGIT )) |
| 4049 | * ; e.g., Jun 2 |
| 4050 | * |
| 4051 | * HTTP-date is case sensitive. A sender MUST NOT generate additional |
| 4052 | * whitespace in an HTTP-date beyond that specifically included as SP in |
| 4053 | * the grammar. |
| 4054 | */ |
| 4055 | int parse_asctime_date(const char *date, int len, struct tm *tm) |
| 4056 | { |
David Carlier | 327298c | 2016-11-20 10:42:38 +0000 | [diff] [blame] | 4057 | /* tm_gmtoff, if present, ought to be zero'ed */ |
| 4058 | memset(tm, 0, sizeof(*tm)); |
| 4059 | |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 4060 | RET0_UNLESS(parse_http_dayname(&date, &len, tm)); /* day-name */ |
| 4061 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4062 | RET0_UNLESS(parse_http_monthname(&date, &len, tm)); /* expect month */ |
| 4063 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4064 | |
| 4065 | /* expect SP and 1DIGIT or 2DIGIT */ |
| 4066 | if (parse_expect_char(&date, &len, ' ')) |
| 4067 | RET0_UNLESS(parse_digit(&date, &len, &tm->tm_mday)); |
| 4068 | else |
| 4069 | RET0_UNLESS(parse_2digit(&date, &len, &tm->tm_mday)); |
| 4070 | |
| 4071 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4072 | RET0_UNLESS(parse_http_time(&date, &len, tm)); /* Parse time. */ |
| 4073 | RET0_UNLESS(parse_expect_char(&date, &len, ' ')); /* expect SP */ |
| 4074 | RET0_UNLESS(parse_4digit(&date, &len, &tm->tm_year)); /* year = 4DIGIT */ |
| 4075 | tm->tm_year -= 1900; |
| 4076 | tm->tm_isdst = -1; |
Thierry Fournier | 9312794 | 2016-01-20 18:49:45 +0100 | [diff] [blame] | 4077 | return 1; |
| 4078 | } |
| 4079 | |
| 4080 | /* From RFC7231 |
| 4081 | * https://tools.ietf.org/html/rfc7231#section-7.1.1.1 |
| 4082 | * |
| 4083 | * HTTP-date = IMF-fixdate / obs-date |
| 4084 | * obs-date = rfc850-date / asctime-date |
| 4085 | * |
| 4086 | * parses an HTTP date in the RFC format and is accepted |
| 4087 | * alternatives. <date> is the strinf containing the date, |
| 4088 | * len is the len of the string. <tm> is filled with the |
| 4089 | * parsed time. We must considers this time as GMT. |
| 4090 | */ |
| 4091 | int parse_http_date(const char *date, int len, struct tm *tm) |
| 4092 | { |
| 4093 | if (parse_imf_date(date, len, tm)) |
| 4094 | return 1; |
| 4095 | |
| 4096 | if (parse_rfc850_date(date, len, tm)) |
| 4097 | return 1; |
| 4098 | |
| 4099 | if (parse_asctime_date(date, len, tm)) |
| 4100 | return 1; |
| 4101 | |
| 4102 | return 0; |
| 4103 | } |
| 4104 | |
Willy Tarreau | 4deeb10 | 2021-01-29 10:47:52 +0100 | [diff] [blame] | 4105 | /* print the time <ns> in a short form (exactly 7 chars) at the end of buffer |
| 4106 | * <out>. "-" is printed if the value is zero, "inf" if larger than 1000 years. |
| 4107 | * It returns the new buffer length, or 0 if it doesn't fit. The value will be |
| 4108 | * surrounded by <pfx> and <sfx> respectively if not NULL. |
| 4109 | */ |
| 4110 | int print_time_short(struct buffer *out, const char *pfx, uint64_t ns, const char *sfx) |
| 4111 | { |
| 4112 | double val = ns; // 52 bits of mantissa keep ns accuracy over 52 days |
| 4113 | const char *unit; |
| 4114 | |
| 4115 | if (!pfx) |
| 4116 | pfx = ""; |
| 4117 | if (!sfx) |
| 4118 | sfx = ""; |
| 4119 | |
| 4120 | do { |
| 4121 | unit = " - "; if (val <= 0.0) break; |
| 4122 | unit = "ns"; if (val < 1000.0) break; |
| 4123 | unit = "us"; val /= 1000.0; if (val < 1000.0) break; |
| 4124 | unit = "ms"; val /= 1000.0; if (val < 1000.0) break; |
| 4125 | unit = "s "; val /= 1000.0; if (val < 60.0) break; |
| 4126 | unit = "m "; val /= 60.0; if (val < 60.0) break; |
| 4127 | unit = "h "; val /= 60.0; if (val < 24.0) break; |
| 4128 | unit = "d "; val /= 24.0; if (val < 365.0) break; |
| 4129 | unit = "yr"; val /= 365.0; if (val < 1000.0) break; |
| 4130 | unit = " inf "; val = 0.0; break; |
| 4131 | } while (0); |
| 4132 | |
| 4133 | if (val <= 0.0) |
| 4134 | return chunk_appendf(out, "%s%7s%s", pfx, unit, sfx); |
| 4135 | else if (val < 10.0) |
| 4136 | return chunk_appendf(out, "%s%1.3f%s%s", pfx, val, unit, sfx); |
| 4137 | else if (val < 100.0) |
| 4138 | return chunk_appendf(out, "%s%2.2f%s%s", pfx, val, unit, sfx); |
| 4139 | else |
| 4140 | return chunk_appendf(out, "%s%3.1f%s%s", pfx, val, unit, sfx); |
| 4141 | } |
| 4142 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4143 | /* Dynamically allocates a string of the proper length to hold the formatted |
| 4144 | * output. NULL is returned on error. The caller is responsible for freeing the |
| 4145 | * memory area using free(). The resulting string is returned in <out> if the |
| 4146 | * pointer is not NULL. A previous version of <out> might be used to build the |
| 4147 | * new string, and it will be freed before returning if it is not NULL, which |
| 4148 | * makes it possible to build complex strings from iterative calls without |
| 4149 | * having to care about freeing intermediate values, as in the example below : |
| 4150 | * |
| 4151 | * memprintf(&err, "invalid argument: '%s'", arg); |
| 4152 | * ... |
| 4153 | * memprintf(&err, "parser said : <%s>\n", *err); |
| 4154 | * ... |
| 4155 | * free(*err); |
| 4156 | * |
| 4157 | * This means that <err> must be initialized to NULL before first invocation. |
| 4158 | * The return value also holds the allocated string, which eases error checking |
| 4159 | * 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] | 4160 | * passed instead and it will be ignored. The returned message will then also |
| 4161 | * 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] | 4162 | * |
| 4163 | * It is also convenient to use it without any free except the last one : |
| 4164 | * err = NULL; |
| 4165 | * if (!fct1(err)) report(*err); |
| 4166 | * if (!fct2(err)) report(*err); |
| 4167 | * if (!fct3(err)) report(*err); |
| 4168 | * free(*err); |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 4169 | * |
| 4170 | * memprintf relies on memvprintf. This last version can be called from any |
| 4171 | * function with variadic arguments. |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4172 | */ |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 4173 | char *memvprintf(char **out, const char *format, va_list orig_args) |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4174 | { |
| 4175 | va_list args; |
| 4176 | char *ret = NULL; |
| 4177 | int allocated = 0; |
| 4178 | int needed = 0; |
| 4179 | |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 4180 | if (!out) |
| 4181 | return NULL; |
| 4182 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4183 | do { |
Willy Tarreau | e0609f5 | 2019-03-29 19:13:23 +0100 | [diff] [blame] | 4184 | char buf1; |
| 4185 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4186 | /* vsnprintf() will return the required length even when the |
| 4187 | * target buffer is NULL. We do this in a loop just in case |
| 4188 | * intermediate evaluations get wrong. |
| 4189 | */ |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 4190 | va_copy(args, orig_args); |
Willy Tarreau | e0609f5 | 2019-03-29 19:13:23 +0100 | [diff] [blame] | 4191 | needed = vsnprintf(ret ? ret : &buf1, allocated, format, args); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4192 | va_end(args); |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 4193 | if (needed < allocated) { |
| 4194 | /* Note: on Solaris 8, the first iteration always |
| 4195 | * returns -1 if allocated is zero, so we force a |
| 4196 | * retry. |
| 4197 | */ |
| 4198 | if (!allocated) |
| 4199 | needed = 0; |
| 4200 | else |
| 4201 | break; |
| 4202 | } |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4203 | |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 4204 | allocated = needed + 1; |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 4205 | ret = my_realloc2(ret, allocated); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4206 | } while (ret); |
| 4207 | |
| 4208 | if (needed < 0) { |
| 4209 | /* an error was encountered */ |
Willy Tarreau | 61cfdf4 | 2021-02-20 10:46:51 +0100 | [diff] [blame] | 4210 | ha_free(&ret); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 4211 | } |
| 4212 | |
| 4213 | if (out) { |
| 4214 | free(*out); |
| 4215 | *out = ret; |
| 4216 | } |
| 4217 | |
| 4218 | return ret; |
| 4219 | } |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 4220 | |
Christopher Faulet | 93a518f | 2017-10-24 11:25:33 +0200 | [diff] [blame] | 4221 | char *memprintf(char **out, const char *format, ...) |
| 4222 | { |
| 4223 | va_list args; |
| 4224 | char *ret = NULL; |
| 4225 | |
| 4226 | va_start(args, format); |
| 4227 | ret = memvprintf(out, format, args); |
| 4228 | va_end(args); |
| 4229 | |
| 4230 | return ret; |
| 4231 | } |
| 4232 | |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 4233 | /* Used to add <level> spaces before each line of <out>, unless there is only one line. |
| 4234 | * 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] | 4235 | * freed by the caller. It also supports being passed a NULL which results in the same |
| 4236 | * output. |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 4237 | * Example of use : |
| 4238 | * parse(cmd, &err); (callee: memprintf(&err, ...)) |
| 4239 | * fprintf(stderr, "Parser said: %s\n", indent_error(&err)); |
| 4240 | * free(err); |
| 4241 | */ |
| 4242 | char *indent_msg(char **out, int level) |
| 4243 | { |
| 4244 | char *ret, *in, *p; |
| 4245 | int needed = 0; |
| 4246 | int lf = 0; |
| 4247 | int lastlf = 0; |
| 4248 | int len; |
| 4249 | |
Willy Tarreau | 70eec38 | 2012-10-10 08:56:47 +0200 | [diff] [blame] | 4250 | if (!out || !*out) |
| 4251 | return NULL; |
| 4252 | |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 4253 | in = *out - 1; |
| 4254 | while ((in = strchr(in + 1, '\n')) != NULL) { |
| 4255 | lastlf = in - *out; |
| 4256 | lf++; |
| 4257 | } |
| 4258 | |
| 4259 | if (!lf) /* single line, no LF, return it as-is */ |
| 4260 | return *out; |
| 4261 | |
| 4262 | len = strlen(*out); |
| 4263 | |
| 4264 | if (lf == 1 && lastlf == len - 1) { |
| 4265 | /* single line, LF at end, strip it and return as-is */ |
| 4266 | (*out)[lastlf] = 0; |
| 4267 | return *out; |
| 4268 | } |
| 4269 | |
| 4270 | /* OK now we have at least one LF, we need to process the whole string |
| 4271 | * as a multi-line string. What we'll do : |
| 4272 | * - prefix with an LF if there is none |
| 4273 | * - add <level> spaces before each line |
| 4274 | * This means at most ( 1 + level + (len-lf) + lf*<1+level) ) = |
| 4275 | * 1 + level + len + lf * level = 1 + level * (lf + 1) + len. |
| 4276 | */ |
| 4277 | |
| 4278 | needed = 1 + level * (lf + 1) + len + 1; |
| 4279 | p = ret = malloc(needed); |
| 4280 | in = *out; |
| 4281 | |
| 4282 | /* skip initial LFs */ |
| 4283 | while (*in == '\n') |
| 4284 | in++; |
| 4285 | |
| 4286 | /* copy each line, prefixed with LF and <level> spaces, and without the trailing LF */ |
| 4287 | while (*in) { |
| 4288 | *p++ = '\n'; |
| 4289 | memset(p, ' ', level); |
| 4290 | p += level; |
| 4291 | do { |
| 4292 | *p++ = *in++; |
| 4293 | } while (*in && *in != '\n'); |
| 4294 | if (*in) |
| 4295 | in++; |
| 4296 | } |
| 4297 | *p = 0; |
| 4298 | |
| 4299 | free(*out); |
| 4300 | *out = ret; |
| 4301 | |
| 4302 | return ret; |
| 4303 | } |
| 4304 | |
Willy Tarreau | a2c9911 | 2019-08-21 13:17:37 +0200 | [diff] [blame] | 4305 | /* makes a copy of message <in> into <out>, with each line prefixed with <pfx> |
| 4306 | * and end of lines replaced with <eol> if not 0. The first line to indent has |
| 4307 | * to be indicated in <first> (starts at zero), so that it is possible to skip |
| 4308 | * indenting the first line if it has to be appended after an existing message. |
| 4309 | * Empty strings are never indented, and NULL strings are considered empty both |
| 4310 | * for <in> and <pfx>. It returns non-zero if an EOL was appended as the last |
| 4311 | * character, non-zero otherwise. |
| 4312 | */ |
| 4313 | int append_prefixed_str(struct buffer *out, const char *in, const char *pfx, char eol, int first) |
| 4314 | { |
| 4315 | int bol, lf; |
| 4316 | int pfxlen = pfx ? strlen(pfx) : 0; |
| 4317 | |
| 4318 | if (!in) |
| 4319 | return 0; |
| 4320 | |
| 4321 | bol = 1; |
| 4322 | lf = 0; |
| 4323 | while (*in) { |
| 4324 | if (bol && pfxlen) { |
| 4325 | if (first > 0) |
| 4326 | first--; |
| 4327 | else |
| 4328 | b_putblk(out, pfx, pfxlen); |
| 4329 | bol = 0; |
| 4330 | } |
| 4331 | |
| 4332 | lf = (*in == '\n'); |
| 4333 | bol |= lf; |
| 4334 | b_putchr(out, (lf && eol) ? eol : *in); |
| 4335 | in++; |
| 4336 | } |
| 4337 | return lf; |
| 4338 | } |
| 4339 | |
Willy Tarreau | 9d22e56 | 2019-03-29 18:49:09 +0100 | [diff] [blame] | 4340 | /* removes environment variable <name> from the environment as found in |
| 4341 | * environ. This is only provided as an alternative for systems without |
| 4342 | * unsetenv() (old Solaris and AIX versions). THIS IS NOT THREAD SAFE. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4343 | * The principle is to scan environ for each occurrence of variable name |
Willy Tarreau | 9d22e56 | 2019-03-29 18:49:09 +0100 | [diff] [blame] | 4344 | * <name> and to replace the matching pointers with the last pointer of |
| 4345 | * the array (since variables are not ordered). |
| 4346 | * It always returns 0 (success). |
| 4347 | */ |
| 4348 | int my_unsetenv(const char *name) |
| 4349 | { |
| 4350 | extern char **environ; |
| 4351 | char **p = environ; |
| 4352 | int vars; |
| 4353 | int next; |
| 4354 | int len; |
| 4355 | |
| 4356 | len = strlen(name); |
| 4357 | for (vars = 0; p[vars]; vars++) |
| 4358 | ; |
| 4359 | next = 0; |
| 4360 | while (next < vars) { |
| 4361 | if (strncmp(p[next], name, len) != 0 || p[next][len] != '=') { |
| 4362 | next++; |
| 4363 | continue; |
| 4364 | } |
| 4365 | if (next < vars - 1) |
| 4366 | p[next] = p[vars - 1]; |
| 4367 | p[--vars] = NULL; |
| 4368 | } |
| 4369 | return 0; |
| 4370 | } |
| 4371 | |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 4372 | /* Convert occurrences of environment variables in the input string to their |
| 4373 | * corresponding value. A variable is identified as a series of alphanumeric |
| 4374 | * characters or underscores following a '$' sign. The <in> string must be |
| 4375 | * free()able. NULL returns NULL. The resulting string might be reallocated if |
| 4376 | * some expansion is made. Variable names may also be enclosed into braces if |
| 4377 | * needed (eg: to concatenate alphanum characters). |
| 4378 | */ |
| 4379 | char *env_expand(char *in) |
| 4380 | { |
| 4381 | char *txt_beg; |
| 4382 | char *out; |
| 4383 | char *txt_end; |
| 4384 | char *var_beg; |
| 4385 | char *var_end; |
| 4386 | char *value; |
| 4387 | char *next; |
| 4388 | int out_len; |
| 4389 | int val_len; |
| 4390 | |
| 4391 | if (!in) |
| 4392 | return in; |
| 4393 | |
| 4394 | value = out = NULL; |
| 4395 | out_len = 0; |
| 4396 | |
| 4397 | txt_beg = in; |
| 4398 | do { |
| 4399 | /* look for next '$' sign in <in> */ |
| 4400 | for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++); |
| 4401 | |
| 4402 | if (!*txt_end && !out) /* end and no expansion performed */ |
| 4403 | return in; |
| 4404 | |
| 4405 | val_len = 0; |
| 4406 | next = txt_end; |
| 4407 | if (*txt_end == '$') { |
| 4408 | char save; |
| 4409 | |
| 4410 | var_beg = txt_end + 1; |
| 4411 | if (*var_beg == '{') |
| 4412 | var_beg++; |
| 4413 | |
| 4414 | var_end = var_beg; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4415 | while (isalnum((unsigned char)*var_end) || *var_end == '_') { |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 4416 | var_end++; |
| 4417 | } |
| 4418 | |
| 4419 | next = var_end; |
| 4420 | if (*var_end == '}' && (var_beg > txt_end + 1)) |
| 4421 | next++; |
| 4422 | |
| 4423 | /* get value of the variable name at this location */ |
| 4424 | save = *var_end; |
| 4425 | *var_end = '\0'; |
| 4426 | value = getenv(var_beg); |
| 4427 | *var_end = save; |
| 4428 | val_len = value ? strlen(value) : 0; |
| 4429 | } |
| 4430 | |
Hubert Verstraete | 831962e | 2016-06-28 22:44:26 +0200 | [diff] [blame] | 4431 | 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] | 4432 | if (txt_end > txt_beg) { |
| 4433 | memcpy(out + out_len, txt_beg, txt_end - txt_beg); |
| 4434 | out_len += txt_end - txt_beg; |
| 4435 | } |
| 4436 | if (val_len) { |
| 4437 | memcpy(out + out_len, value, val_len); |
| 4438 | out_len += val_len; |
| 4439 | } |
| 4440 | out[out_len] = 0; |
| 4441 | txt_beg = next; |
| 4442 | } while (*txt_beg); |
| 4443 | |
| 4444 | /* here we know that <out> was allocated and that we don't need <in> anymore */ |
| 4445 | free(in); |
| 4446 | return out; |
| 4447 | } |
| 4448 | |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4449 | |
| 4450 | /* same as strstr() but case-insensitive and with limit length */ |
| 4451 | const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2) |
| 4452 | { |
| 4453 | char *pptr, *sptr, *start; |
Willy Tarreau | c874653 | 2014-05-28 23:05:07 +0200 | [diff] [blame] | 4454 | unsigned int slen, plen; |
| 4455 | unsigned int tmp1, tmp2; |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4456 | |
| 4457 | if (str1 == NULL || len_str1 == 0) // search pattern into an empty string => search is not found |
| 4458 | return NULL; |
| 4459 | |
| 4460 | if (str2 == NULL || len_str2 == 0) // pattern is empty => every str1 match |
| 4461 | return str1; |
| 4462 | |
| 4463 | if (len_str1 < len_str2) // pattern is longer than string => search is not found |
| 4464 | return NULL; |
| 4465 | |
| 4466 | 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] | 4467 | while (toupper((unsigned char)*start) != toupper((unsigned char)*str2)) { |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4468 | start++; |
| 4469 | slen--; |
| 4470 | tmp1++; |
| 4471 | |
| 4472 | if (tmp1 >= len_str1) |
| 4473 | return NULL; |
| 4474 | |
| 4475 | /* if pattern longer than string */ |
| 4476 | if (slen < plen) |
| 4477 | return NULL; |
| 4478 | } |
| 4479 | |
| 4480 | sptr = start; |
| 4481 | pptr = (char *)str2; |
| 4482 | |
| 4483 | tmp2 = 0; |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 4484 | while (toupper((unsigned char)*sptr) == toupper((unsigned char)*pptr)) { |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 4485 | sptr++; |
| 4486 | pptr++; |
| 4487 | tmp2++; |
| 4488 | |
| 4489 | if (*pptr == '\0' || tmp2 == len_str2) /* end of pattern found */ |
| 4490 | return start; |
| 4491 | if (*sptr == '\0' || tmp2 == len_str1) /* end of string found and the pattern is not fully found */ |
| 4492 | return NULL; |
| 4493 | } |
| 4494 | } |
| 4495 | return NULL; |
| 4496 | } |
| 4497 | |
Willy Tarreau | 3ff476e | 2022-03-30 10:02:56 +0200 | [diff] [blame] | 4498 | /* Returns true if s1 < s2 < s3 otherwise zero. Both s1 and s3 may be NULL and |
| 4499 | * in this case only non-null strings are compared. This allows to pass initial |
| 4500 | * values in iterators and in sort functions. |
| 4501 | */ |
| 4502 | int strordered(const char *s1, const char *s2, const char *s3) |
| 4503 | { |
| 4504 | return (!s1 || strcmp(s1, s2) < 0) && (!s3 || strcmp(s2, s3) < 0); |
| 4505 | } |
| 4506 | |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 4507 | /* This function read the next valid utf8 char. |
| 4508 | * <s> is the byte srray to be decode, <len> is its length. |
| 4509 | * The function returns decoded char encoded like this: |
| 4510 | * The 4 msb are the return code (UTF8_CODE_*), the 4 lsb |
| 4511 | * are the length read. The decoded character is stored in <c>. |
| 4512 | */ |
| 4513 | unsigned char utf8_next(const char *s, int len, unsigned int *c) |
| 4514 | { |
| 4515 | const unsigned char *p = (unsigned char *)s; |
| 4516 | int dec; |
| 4517 | unsigned char code = UTF8_CODE_OK; |
| 4518 | |
| 4519 | if (len < 1) |
| 4520 | return UTF8_CODE_OK; |
| 4521 | |
| 4522 | /* Check the type of UTF8 sequence |
| 4523 | * |
| 4524 | * 0... .... 0x00 <= x <= 0x7f : 1 byte: ascii char |
| 4525 | * 10.. .... 0x80 <= x <= 0xbf : invalid sequence |
| 4526 | * 110. .... 0xc0 <= x <= 0xdf : 2 bytes |
| 4527 | * 1110 .... 0xe0 <= x <= 0xef : 3 bytes |
| 4528 | * 1111 0... 0xf0 <= x <= 0xf7 : 4 bytes |
| 4529 | * 1111 10.. 0xf8 <= x <= 0xfb : 5 bytes |
| 4530 | * 1111 110. 0xfc <= x <= 0xfd : 6 bytes |
| 4531 | * 1111 111. 0xfe <= x <= 0xff : invalid sequence |
| 4532 | */ |
| 4533 | switch (*p) { |
| 4534 | case 0x00 ... 0x7f: |
| 4535 | *c = *p; |
| 4536 | return UTF8_CODE_OK | 1; |
| 4537 | |
| 4538 | case 0x80 ... 0xbf: |
| 4539 | *c = *p; |
| 4540 | return UTF8_CODE_BADSEQ | 1; |
| 4541 | |
| 4542 | case 0xc0 ... 0xdf: |
| 4543 | if (len < 2) { |
| 4544 | *c = *p; |
| 4545 | return UTF8_CODE_BADSEQ | 1; |
| 4546 | } |
| 4547 | *c = *p & 0x1f; |
| 4548 | dec = 1; |
| 4549 | break; |
| 4550 | |
| 4551 | case 0xe0 ... 0xef: |
| 4552 | if (len < 3) { |
| 4553 | *c = *p; |
| 4554 | return UTF8_CODE_BADSEQ | 1; |
| 4555 | } |
| 4556 | *c = *p & 0x0f; |
| 4557 | dec = 2; |
| 4558 | break; |
| 4559 | |
| 4560 | case 0xf0 ... 0xf7: |
| 4561 | if (len < 4) { |
| 4562 | *c = *p; |
| 4563 | return UTF8_CODE_BADSEQ | 1; |
| 4564 | } |
| 4565 | *c = *p & 0x07; |
| 4566 | dec = 3; |
| 4567 | break; |
| 4568 | |
| 4569 | case 0xf8 ... 0xfb: |
| 4570 | if (len < 5) { |
| 4571 | *c = *p; |
| 4572 | return UTF8_CODE_BADSEQ | 1; |
| 4573 | } |
| 4574 | *c = *p & 0x03; |
| 4575 | dec = 4; |
| 4576 | break; |
| 4577 | |
| 4578 | case 0xfc ... 0xfd: |
| 4579 | if (len < 6) { |
| 4580 | *c = *p; |
| 4581 | return UTF8_CODE_BADSEQ | 1; |
| 4582 | } |
| 4583 | *c = *p & 0x01; |
| 4584 | dec = 5; |
| 4585 | break; |
| 4586 | |
| 4587 | case 0xfe ... 0xff: |
| 4588 | default: |
| 4589 | *c = *p; |
| 4590 | return UTF8_CODE_BADSEQ | 1; |
| 4591 | } |
| 4592 | |
| 4593 | p++; |
| 4594 | |
| 4595 | while (dec > 0) { |
| 4596 | |
| 4597 | /* need 0x10 for the 2 first bits */ |
| 4598 | if ( ( *p & 0xc0 ) != 0x80 ) |
| 4599 | return UTF8_CODE_BADSEQ | ((p-(unsigned char *)s)&0xffff); |
| 4600 | |
| 4601 | /* add data at char */ |
| 4602 | *c = ( *c << 6 ) | ( *p & 0x3f ); |
| 4603 | |
| 4604 | dec--; |
| 4605 | p++; |
| 4606 | } |
| 4607 | |
| 4608 | /* Check ovelong encoding. |
| 4609 | * 1 byte : 5 + 6 : 11 : 0x80 ... 0x7ff |
| 4610 | * 2 bytes : 4 + 6 + 6 : 16 : 0x800 ... 0xffff |
| 4611 | * 3 bytes : 3 + 6 + 6 + 6 : 21 : 0x10000 ... 0x1fffff |
| 4612 | */ |
Thierry FOURNIER | 9e7ec08 | 2015-03-12 19:32:38 +0100 | [diff] [blame] | 4613 | if (( *c <= 0x7f && (p-(unsigned char *)s) > 1) || |
Thierry FOURNIER | 317e1c4 | 2014-08-12 10:20:47 +0200 | [diff] [blame] | 4614 | (*c >= 0x80 && *c <= 0x7ff && (p-(unsigned char *)s) > 2) || |
| 4615 | (*c >= 0x800 && *c <= 0xffff && (p-(unsigned char *)s) > 3) || |
| 4616 | (*c >= 0x10000 && *c <= 0x1fffff && (p-(unsigned char *)s) > 4)) |
| 4617 | code |= UTF8_CODE_OVERLONG; |
| 4618 | |
| 4619 | /* Check invalid UTF8 range. */ |
| 4620 | if ((*c >= 0xd800 && *c <= 0xdfff) || |
| 4621 | (*c >= 0xfffe && *c <= 0xffff)) |
| 4622 | code |= UTF8_CODE_INVRANGE; |
| 4623 | |
| 4624 | return code | ((p-(unsigned char *)s)&0x0f); |
| 4625 | } |
| 4626 | |
Maxime de Roucy | dc88785 | 2016-05-13 23:52:54 +0200 | [diff] [blame] | 4627 | /* append a copy of string <str> (in a wordlist) at the end of the list <li> |
| 4628 | * On failure : return 0 and <err> filled with an error message. |
| 4629 | * The caller is responsible for freeing the <err> and <str> copy |
| 4630 | * memory area using free() |
| 4631 | */ |
| 4632 | int list_append_word(struct list *li, const char *str, char **err) |
| 4633 | { |
| 4634 | struct wordlist *wl; |
| 4635 | |
| 4636 | wl = calloc(1, sizeof(*wl)); |
| 4637 | if (!wl) { |
| 4638 | memprintf(err, "out of memory"); |
| 4639 | goto fail_wl; |
| 4640 | } |
| 4641 | |
| 4642 | wl->s = strdup(str); |
| 4643 | if (!wl->s) { |
| 4644 | memprintf(err, "out of memory"); |
| 4645 | goto fail_wl_s; |
| 4646 | } |
| 4647 | |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 4648 | LIST_APPEND(li, &wl->list); |
Maxime de Roucy | dc88785 | 2016-05-13 23:52:54 +0200 | [diff] [blame] | 4649 | |
| 4650 | return 1; |
| 4651 | |
| 4652 | fail_wl_s: |
| 4653 | free(wl->s); |
| 4654 | fail_wl: |
| 4655 | free(wl); |
| 4656 | return 0; |
| 4657 | } |
| 4658 | |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4659 | /* indicates if a memory location may safely be read or not. The trick consists |
| 4660 | * in performing a harmless syscall using this location as an input and letting |
| 4661 | * the operating system report whether it's OK or not. For this we have the |
| 4662 | * stat() syscall, which will return EFAULT when the memory location supposed |
| 4663 | * to contain the file name is not readable. If it is readable it will then |
| 4664 | * either return 0 if the area contains an existing file name, or -1 with |
| 4665 | * another code. This must not be abused, and some audit systems might detect |
| 4666 | * this as abnormal activity. It's used only for unsafe dumps. |
| 4667 | */ |
| 4668 | int may_access(const void *ptr) |
| 4669 | { |
| 4670 | struct stat buf; |
| 4671 | |
| 4672 | if (stat(ptr, &buf) == 0) |
| 4673 | return 1; |
| 4674 | if (errno == EFAULT) |
| 4675 | return 0; |
| 4676 | return 1; |
| 4677 | } |
| 4678 | |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4679 | /* print a string of text buffer to <out>. The format is : |
| 4680 | * Non-printable chars \t, \n, \r and \e are * encoded in C format. |
| 4681 | * Other non-printable chars are encoded "\xHH". Space, '\', and '=' are also escaped. |
| 4682 | * Print stopped if null char or <bsize> is reached, or if no more place in the chunk. |
| 4683 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4684 | int dump_text(struct buffer *out, const char *buf, int bsize) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4685 | { |
| 4686 | unsigned char c; |
Tim Duesterhus | 18795d4 | 2021-08-29 00:58:22 +0200 | [diff] [blame] | 4687 | size_t ptr = 0; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4688 | |
Tim Duesterhus | 18795d4 | 2021-08-29 00:58:22 +0200 | [diff] [blame] | 4689 | while (ptr < bsize && buf[ptr]) { |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4690 | c = buf[ptr]; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4691 | if (isprint((unsigned char)c) && isascii((unsigned char)c) && c != '\\' && c != ' ' && c != '=') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4692 | if (out->data > out->size - 1) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4693 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4694 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4695 | } |
| 4696 | 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] | 4697 | if (out->data > out->size - 2) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4698 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4699 | out->area[out->data++] = '\\'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4700 | switch (c) { |
| 4701 | case ' ': c = ' '; break; |
| 4702 | case '\t': c = 't'; break; |
| 4703 | case '\n': c = 'n'; break; |
| 4704 | case '\r': c = 'r'; break; |
| 4705 | case '\e': c = 'e'; break; |
| 4706 | case '\\': c = '\\'; break; |
| 4707 | case '=': c = '='; break; |
| 4708 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4709 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4710 | } |
| 4711 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4712 | if (out->data > out->size - 4) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4713 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4714 | out->area[out->data++] = '\\'; |
| 4715 | out->area[out->data++] = 'x'; |
| 4716 | out->area[out->data++] = hextab[(c >> 4) & 0xF]; |
| 4717 | out->area[out->data++] = hextab[c & 0xF]; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4718 | } |
| 4719 | ptr++; |
| 4720 | } |
| 4721 | |
| 4722 | return ptr; |
| 4723 | } |
| 4724 | |
| 4725 | /* print a buffer in hexa. |
| 4726 | * Print stopped if <bsize> is reached, or if no more place in the chunk. |
| 4727 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4728 | int dump_binary(struct buffer *out, const char *buf, int bsize) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4729 | { |
| 4730 | unsigned char c; |
| 4731 | int ptr = 0; |
| 4732 | |
| 4733 | while (ptr < bsize) { |
| 4734 | c = buf[ptr]; |
| 4735 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4736 | if (out->data > out->size - 2) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4737 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4738 | out->area[out->data++] = hextab[(c >> 4) & 0xF]; |
| 4739 | out->area[out->data++] = hextab[c & 0xF]; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4740 | |
| 4741 | ptr++; |
| 4742 | } |
| 4743 | return ptr; |
| 4744 | } |
| 4745 | |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4746 | /* Appends into buffer <out> a hex dump of memory area <buf> for <len> bytes, |
| 4747 | * prepending each line with prefix <pfx>. The output is *not* initialized. |
| 4748 | * The output will not wrap pas the buffer's end so it is more optimal if the |
| 4749 | * caller makes sure the buffer is aligned first. A trailing zero will always |
| 4750 | * 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] | 4751 | * sure that the area is dumpable first. If <unsafe> is non-null, the memory |
| 4752 | * locations are checked first for being readable. |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4753 | */ |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4754 | 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] | 4755 | { |
| 4756 | const unsigned char *d = buf; |
| 4757 | int i, j, start; |
| 4758 | |
| 4759 | d = (const unsigned char *)(((unsigned long)buf) & -16); |
| 4760 | start = ((unsigned long)buf) & 15; |
| 4761 | |
| 4762 | for (i = 0; i < start + len; i += 16) { |
| 4763 | chunk_appendf(out, (sizeof(void *) == 4) ? "%s%8p: " : "%s%16p: ", pfx, d + i); |
| 4764 | |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4765 | // 0: unchecked, 1: checked safe, 2: danger |
| 4766 | unsafe = !!unsafe; |
| 4767 | if (unsafe && !may_access(d + i)) |
| 4768 | unsafe = 2; |
| 4769 | |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4770 | for (j = 0; j < 16; j++) { |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4771 | if ((i + j < start) || (i + j >= start + len)) |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4772 | chunk_strcat(out, "'' "); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4773 | else if (unsafe > 1) |
| 4774 | chunk_strcat(out, "** "); |
| 4775 | else |
| 4776 | chunk_appendf(out, "%02x ", d[i + j]); |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4777 | |
| 4778 | if (j == 7) |
| 4779 | chunk_strcat(out, "- "); |
| 4780 | } |
| 4781 | chunk_strcat(out, " "); |
| 4782 | for (j = 0; j < 16; j++) { |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4783 | if ((i + j < start) || (i + j >= start + len)) |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4784 | chunk_strcat(out, "'"); |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4785 | else if (unsafe > 1) |
| 4786 | chunk_strcat(out, "*"); |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4787 | else if (isprint((unsigned char)d[i + j])) |
Willy Tarreau | 3710105 | 2019-05-20 16:48:20 +0200 | [diff] [blame] | 4788 | chunk_appendf(out, "%c", d[i + j]); |
| 4789 | else |
| 4790 | chunk_strcat(out, "."); |
Willy Tarreau | 9fc5dcb | 2019-05-20 16:13:40 +0200 | [diff] [blame] | 4791 | } |
| 4792 | chunk_strcat(out, "\n"); |
| 4793 | } |
| 4794 | } |
| 4795 | |
Willy Tarreau | 762fb3e | 2020-03-03 15:57:10 +0100 | [diff] [blame] | 4796 | /* dumps <pfx> followed by <n> bytes from <addr> in hex form into buffer <buf> |
| 4797 | * enclosed in brackets after the address itself, formatted on 14 chars |
| 4798 | * including the "0x" prefix. This is meant to be used as a prefix for code |
| 4799 | * areas. For example: |
| 4800 | * "0x7f10b6557690 [48 c7 c0 0f 00 00 00 0f]" |
| 4801 | * It relies on may_access() to know if the bytes are dumpable, otherwise "--" |
| 4802 | * is emitted. A NULL <pfx> will be considered empty. |
| 4803 | */ |
| 4804 | void dump_addr_and_bytes(struct buffer *buf, const char *pfx, const void *addr, int n) |
| 4805 | { |
| 4806 | int ok = 0; |
| 4807 | int i; |
| 4808 | |
| 4809 | chunk_appendf(buf, "%s%#14lx [", pfx ? pfx : "", (long)addr); |
| 4810 | |
| 4811 | for (i = 0; i < n; i++) { |
| 4812 | if (i == 0 || (((long)(addr + i) ^ (long)(addr)) & 4096)) |
| 4813 | ok = may_access(addr + i); |
| 4814 | if (ok) |
| 4815 | chunk_appendf(buf, "%02x%s", ((uint8_t*)addr)[i], (i<n-1) ? " " : "]"); |
| 4816 | else |
| 4817 | chunk_appendf(buf, "--%s", (i<n-1) ? " " : "]"); |
| 4818 | } |
| 4819 | } |
| 4820 | |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4821 | /* print a line of text buffer (limited to 70 bytes) to <out>. The format is : |
| 4822 | * <2 spaces> <offset=5 digits> <space or plus> <space> <70 chars max> <\n> |
| 4823 | * which is 60 chars per line. Non-printable chars \t, \n, \r and \e are |
| 4824 | * encoded in C format. Other non-printable chars are encoded "\xHH". Original |
| 4825 | * lines are respected within the limit of 70 output chars. Lines that are |
| 4826 | * continuation of a previous truncated line begin with "+" instead of " " |
| 4827 | * after the offset. The new pointer is returned. |
| 4828 | */ |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 4829 | 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] | 4830 | int *line, int ptr) |
| 4831 | { |
| 4832 | int end; |
| 4833 | unsigned char c; |
| 4834 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4835 | end = out->data + 80; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4836 | if (end > out->size) |
| 4837 | return ptr; |
| 4838 | |
| 4839 | chunk_appendf(out, " %05d%c ", ptr, (ptr == *line) ? ' ' : '+'); |
| 4840 | |
| 4841 | while (ptr < len && ptr < bsize) { |
| 4842 | c = buf[ptr]; |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 4843 | if (isprint((unsigned char)c) && isascii((unsigned char)c) && c != '\\') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4844 | if (out->data > end - 2) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4845 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4846 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4847 | } else if (c == '\t' || c == '\n' || c == '\r' || c == '\e' || c == '\\') { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4848 | if (out->data > end - 3) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4849 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4850 | out->area[out->data++] = '\\'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4851 | switch (c) { |
| 4852 | case '\t': c = 't'; break; |
| 4853 | case '\n': c = 'n'; break; |
| 4854 | case '\r': c = 'r'; break; |
| 4855 | case '\e': c = 'e'; break; |
| 4856 | case '\\': c = '\\'; break; |
| 4857 | } |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4858 | out->area[out->data++] = c; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4859 | } else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4860 | if (out->data > end - 5) |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4861 | break; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4862 | out->area[out->data++] = '\\'; |
| 4863 | out->area[out->data++] = 'x'; |
| 4864 | out->area[out->data++] = hextab[(c >> 4) & 0xF]; |
| 4865 | out->area[out->data++] = hextab[c & 0xF]; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4866 | } |
| 4867 | if (buf[ptr++] == '\n') { |
| 4868 | /* we had a line break, let's return now */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4869 | out->area[out->data++] = '\n'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4870 | *line = ptr; |
| 4871 | return ptr; |
| 4872 | } |
| 4873 | } |
| 4874 | /* we have an incomplete line, we return it as-is */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4875 | out->area[out->data++] = '\n'; |
Willy Tarreau | 97c2ae1 | 2016-11-22 18:00:20 +0100 | [diff] [blame] | 4876 | return ptr; |
| 4877 | } |
| 4878 | |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4879 | /* 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] | 4880 | * has address <baseaddr>. String <pfx> may be placed as a prefix in front of |
| 4881 | * 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] | 4882 | */ |
Willy Tarreau | ed936c5 | 2017-04-27 18:03:20 +0200 | [diff] [blame] | 4883 | void debug_hexdump(FILE *out, const char *pfx, const char *buf, |
| 4884 | unsigned int baseaddr, int len) |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4885 | { |
Willy Tarreau | 7345979 | 2017-04-11 07:58:08 +0200 | [diff] [blame] | 4886 | unsigned int i; |
| 4887 | int b, j; |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4888 | |
| 4889 | for (i = 0; i < (len + (baseaddr & 15)); i += 16) { |
| 4890 | b = i - (baseaddr & 15); |
Willy Tarreau | ed936c5 | 2017-04-27 18:03:20 +0200 | [diff] [blame] | 4891 | fprintf(out, "%s%08x: ", pfx ? pfx : "", i + (baseaddr & ~15)); |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 4892 | for (j = 0; j < 8; j++) { |
| 4893 | if (b + j >= 0 && b + j < len) |
| 4894 | fprintf(out, "%02x ", (unsigned char)buf[b + j]); |
| 4895 | else |
| 4896 | fprintf(out, " "); |
| 4897 | } |
| 4898 | |
| 4899 | if (b + j >= 0 && b + j < len) |
| 4900 | fputc('-', out); |
| 4901 | else |
| 4902 | fputc(' ', out); |
| 4903 | |
| 4904 | for (j = 8; j < 16; j++) { |
| 4905 | if (b + j >= 0 && b + j < len) |
| 4906 | fprintf(out, " %02x", (unsigned char)buf[b + j]); |
| 4907 | else |
| 4908 | fprintf(out, " "); |
| 4909 | } |
| 4910 | |
| 4911 | fprintf(out, " "); |
| 4912 | for (j = 0; j < 16; j++) { |
| 4913 | if (b + j >= 0 && b + j < len) { |
| 4914 | if (isprint((unsigned char)buf[b + j])) |
| 4915 | fputc((unsigned char)buf[b + j], out); |
| 4916 | else |
| 4917 | fputc('.', out); |
| 4918 | } |
| 4919 | else |
| 4920 | fputc(' ', out); |
| 4921 | } |
| 4922 | fputc('\n', out); |
| 4923 | } |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 4924 | } |
| 4925 | |
Willy Tarreau | bb86986 | 2020-04-16 10:52:41 +0200 | [diff] [blame] | 4926 | /* Tries to report the executable path name on platforms supporting this. If |
| 4927 | * not found or not possible, returns NULL. |
| 4928 | */ |
| 4929 | const char *get_exec_path() |
| 4930 | { |
| 4931 | const char *ret = NULL; |
| 4932 | |
David Carlier | 43a5685 | 2022-03-04 15:50:48 +0000 | [diff] [blame] | 4933 | #if defined(__linux__) && defined(__GLIBC__) && (__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 16)) |
Willy Tarreau | bb86986 | 2020-04-16 10:52:41 +0200 | [diff] [blame] | 4934 | long execfn = getauxval(AT_EXECFN); |
| 4935 | |
| 4936 | if (execfn && execfn != ENOENT) |
| 4937 | ret = (const char *)execfn; |
devnexen@gmail.com | c4e5232 | 2021-08-17 12:55:49 +0100 | [diff] [blame] | 4938 | #elif defined(__FreeBSD__) |
| 4939 | Elf_Auxinfo *auxv; |
| 4940 | for (auxv = __elf_aux_vector; auxv->a_type != AT_NULL; ++auxv) { |
| 4941 | if (auxv->a_type == AT_EXECPATH) { |
| 4942 | ret = (const char *)auxv->a_un.a_ptr; |
| 4943 | break; |
| 4944 | } |
| 4945 | } |
David Carlier | bd2cced | 2021-08-17 08:44:25 +0100 | [diff] [blame] | 4946 | #elif defined(__NetBSD__) |
| 4947 | AuxInfo *auxv; |
| 4948 | for (auxv = _dlauxinfo(); auxv->a_type != AT_NULL; ++auxv) { |
| 4949 | if (auxv->a_type == AT_SUN_EXECNAME) { |
| 4950 | ret = (const char *)auxv->a_v; |
| 4951 | break; |
| 4952 | } |
| 4953 | } |
David Carlier | 7198c70 | 2022-05-14 17:15:49 +0100 | [diff] [blame] | 4954 | #elif defined(__sun) |
| 4955 | ret = getexecname(); |
Willy Tarreau | bb86986 | 2020-04-16 10:52:41 +0200 | [diff] [blame] | 4956 | #endif |
| 4957 | return ret; |
| 4958 | } |
| 4959 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 4960 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 4961 | /* calls dladdr() or dladdr1() on <addr> and <dli>. If dladdr1 is available, |
| 4962 | * also returns the symbol size in <size>, otherwise returns 0 there. |
| 4963 | */ |
| 4964 | static int dladdr_and_size(const void *addr, Dl_info *dli, size_t *size) |
| 4965 | { |
| 4966 | int ret; |
Willy Tarreau | 7b2108c | 2021-08-30 10:15:35 +0200 | [diff] [blame] | 4967 | #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] | 4968 | const ElfW(Sym) *sym __attribute__((may_alias)); |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 4969 | |
| 4970 | ret = dladdr1(addr, dli, (void **)&sym, RTLD_DL_SYMENT); |
| 4971 | if (ret) |
| 4972 | *size = sym ? sym->st_size : 0; |
| 4973 | #else |
David Carlier | ae5c42f | 2021-12-31 08:15:29 +0000 | [diff] [blame] | 4974 | #if defined(__sun) |
| 4975 | ret = dladdr((void *)addr, dli); |
| 4976 | #else |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 4977 | ret = dladdr(addr, dli); |
David Carlier | ae5c42f | 2021-12-31 08:15:29 +0000 | [diff] [blame] | 4978 | #endif |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 4979 | *size = 0; |
| 4980 | #endif |
| 4981 | return ret; |
| 4982 | } |
Willy Tarreau | 6419239 | 2021-05-05 09:06:21 +0200 | [diff] [blame] | 4983 | |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 4984 | /* Sets build_is_static to true if we detect a static build. Some older glibcs |
| 4985 | * tend to crash inside dlsym() in static builds, but tests show that at least |
| 4986 | * dladdr() still works (and will fail to resolve anything of course). Thus we |
| 4987 | * try to determine if we're on a static build to avoid calling dlsym() in this |
| 4988 | * case. |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 4989 | */ |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 4990 | void check_if_static_build() |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 4991 | { |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 4992 | Dl_info dli = { }; |
| 4993 | size_t size = 0; |
| 4994 | |
| 4995 | /* Now let's try to be smarter */ |
| 4996 | if (!dladdr_and_size(&main, &dli, &size)) |
| 4997 | build_is_static = 1; |
| 4998 | else |
| 4999 | build_is_static = 0; |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 5000 | } |
| 5001 | |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 5002 | INITCALL0(STG_PREPARE, check_if_static_build); |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 5003 | |
Willy Tarreau | 6419239 | 2021-05-05 09:06:21 +0200 | [diff] [blame] | 5004 | /* Tries to retrieve the address of the first occurrence symbol <name>. |
| 5005 | * Note that NULL in return is not always an error as a symbol may have that |
| 5006 | * address in special situations. |
| 5007 | */ |
| 5008 | void *get_sym_curr_addr(const char *name) |
| 5009 | { |
| 5010 | void *ptr = NULL; |
| 5011 | |
| 5012 | #ifdef RTLD_DEFAULT |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 5013 | if (!build_is_static) |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 5014 | ptr = dlsym(RTLD_DEFAULT, name); |
Willy Tarreau | 6419239 | 2021-05-05 09:06:21 +0200 | [diff] [blame] | 5015 | #endif |
| 5016 | return ptr; |
| 5017 | } |
| 5018 | |
| 5019 | |
| 5020 | /* Tries to retrieve the address of the next occurrence of symbol <name> |
| 5021 | * Note that NULL in return is not always an error as a symbol may have that |
| 5022 | * address in special situations. |
| 5023 | */ |
| 5024 | void *get_sym_next_addr(const char *name) |
| 5025 | { |
| 5026 | void *ptr = NULL; |
| 5027 | |
| 5028 | #ifdef RTLD_NEXT |
Willy Tarreau | 5b3cd95 | 2022-07-18 13:58:17 +0200 | [diff] [blame] | 5029 | if (!build_is_static) |
Willy Tarreau | 288dc1d | 2022-07-16 13:49:34 +0200 | [diff] [blame] | 5030 | ptr = dlsym(RTLD_NEXT, name); |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5031 | #endif |
Willy Tarreau | 6419239 | 2021-05-05 09:06:21 +0200 | [diff] [blame] | 5032 | return ptr; |
| 5033 | } |
| 5034 | |
| 5035 | #else /* elf & linux & dl */ |
| 5036 | |
| 5037 | /* no possible resolving on other platforms at the moment */ |
| 5038 | void *get_sym_curr_addr(const char *name) |
| 5039 | { |
| 5040 | return NULL; |
| 5041 | } |
| 5042 | |
| 5043 | void *get_sym_next_addr(const char *name) |
| 5044 | { |
| 5045 | return NULL; |
| 5046 | } |
| 5047 | |
| 5048 | #endif /* elf & linux & dl */ |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5049 | |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5050 | /* Tries to append to buffer <buf> some indications about the symbol at address |
| 5051 | * <addr> using the following form: |
| 5052 | * lib:+0xoffset (unresolvable address from lib's base) |
| 5053 | * main+0xoffset (unresolvable address from main (+/-)) |
| 5054 | * lib:main+0xoffset (unresolvable lib address from main (+/-)) |
| 5055 | * name (resolved exact exec address) |
| 5056 | * lib:name (resolved exact lib address) |
| 5057 | * name+0xoffset/0xsize (resolved address within exec symbol) |
| 5058 | * lib:name+0xoffset/0xsize (resolved address within lib symbol) |
| 5059 | * |
| 5060 | * The file name (lib or executable) is limited to what lies between the last |
| 5061 | * '/' and the first following '.'. An optional prefix <pfx> is prepended before |
| 5062 | * 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] | 5063 | * 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] | 5064 | * |
| 5065 | * The symbol's base address is returned, or NULL when unresolved, in order to |
| 5066 | * allow the caller to match it against known ones. |
| 5067 | */ |
Willy Tarreau | 45fd103 | 2021-01-20 14:37:59 +0100 | [diff] [blame] | 5068 | 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] | 5069 | { |
| 5070 | const struct { |
| 5071 | const void *func; |
| 5072 | const char *name; |
| 5073 | } fcts[] = { |
| 5074 | { .func = process_stream, .name = "process_stream" }, |
| 5075 | { .func = task_run_applet, .name = "task_run_applet" }, |
Willy Tarreau | 462b989 | 2022-05-18 18:06:53 +0200 | [diff] [blame] | 5076 | { .func = sc_conn_io_cb, .name = "sc_conn_io_cb" }, |
Willy Tarreau | 586f71b | 2020-12-11 15:54:36 +0100 | [diff] [blame] | 5077 | { .func = sock_conn_iocb, .name = "sock_conn_iocb" }, |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5078 | { .func = dgram_fd_handler, .name = "dgram_fd_handler" }, |
| 5079 | { .func = listener_accept, .name = "listener_accept" }, |
Willy Tarreau | d597ec2 | 2021-01-29 14:29:06 +0100 | [diff] [blame] | 5080 | { .func = manage_global_listener_queue, .name = "manage_global_listener_queue" }, |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5081 | { .func = poller_pipe_io_handler, .name = "poller_pipe_io_handler" }, |
| 5082 | { .func = mworker_accept_wrapper, .name = "mworker_accept_wrapper" }, |
Willy Tarreau | 02922e1 | 2021-01-29 12:27:57 +0100 | [diff] [blame] | 5083 | { .func = session_expire_embryonic, .name = "session_expire_embryonic" }, |
Willy Tarreau | fb5401f | 2021-01-29 12:25:23 +0100 | [diff] [blame] | 5084 | #ifdef USE_THREAD |
| 5085 | { .func = accept_queue_process, .name = "accept_queue_process" }, |
| 5086 | #endif |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5087 | #ifdef USE_LUA |
| 5088 | { .func = hlua_process_task, .name = "hlua_process_task" }, |
| 5089 | #endif |
Ilya Shipitsin | bdec3ba | 2020-11-14 01:56:34 +0500 | [diff] [blame] | 5090 | #ifdef SSL_MODE_ASYNC |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5091 | { .func = ssl_async_fd_free, .name = "ssl_async_fd_free" }, |
| 5092 | { .func = ssl_async_fd_handler, .name = "ssl_async_fd_handler" }, |
| 5093 | #endif |
| 5094 | }; |
| 5095 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 5096 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5097 | Dl_info dli, dli_main; |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5098 | size_t size; |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5099 | const char *fname, *p; |
| 5100 | #endif |
| 5101 | int i; |
| 5102 | |
| 5103 | if (pfx) |
| 5104 | chunk_appendf(buf, "%s", pfx); |
| 5105 | |
| 5106 | for (i = 0; i < sizeof(fcts) / sizeof(fcts[0]); i++) { |
| 5107 | if (addr == fcts[i].func) { |
| 5108 | chunk_appendf(buf, "%s", fcts[i].name); |
| 5109 | return addr; |
| 5110 | } |
| 5111 | } |
| 5112 | |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 5113 | #if (defined(__ELF__) && !defined(__linux__)) || defined(USE_DL) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5114 | /* Now let's try to be smarter */ |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5115 | if (!dladdr_and_size(addr, &dli, &size)) |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5116 | goto unknown; |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5117 | |
| 5118 | /* 1. prefix the library name if it's not the same object as the one |
| 5119 | * that contains the main function. The name is picked between last '/' |
| 5120 | * and first following '.'. |
| 5121 | */ |
| 5122 | if (!dladdr(main, &dli_main)) |
| 5123 | dli_main.dli_fbase = NULL; |
| 5124 | |
| 5125 | if (dli_main.dli_fbase != dli.dli_fbase) { |
| 5126 | fname = dli.dli_fname; |
| 5127 | p = strrchr(fname, '/'); |
| 5128 | if (p++) |
| 5129 | fname = p; |
| 5130 | p = strchr(fname, '.'); |
| 5131 | if (!p) |
| 5132 | p = fname + strlen(fname); |
| 5133 | |
| 5134 | chunk_appendf(buf, "%.*s:", (int)(long)(p - fname), fname); |
| 5135 | } |
| 5136 | |
| 5137 | /* 2. symbol name */ |
| 5138 | if (dli.dli_sname) { |
| 5139 | /* known, dump it and return symbol's address (exact or relative) */ |
| 5140 | chunk_appendf(buf, "%s", dli.dli_sname); |
| 5141 | if (addr != dli.dli_saddr) { |
| 5142 | chunk_appendf(buf, "+%#lx", (long)(addr - dli.dli_saddr)); |
Willy Tarreau | 9133e48 | 2020-03-04 10:19:36 +0100 | [diff] [blame] | 5143 | if (size) |
| 5144 | chunk_appendf(buf, "/%#lx", (long)size); |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5145 | } |
| 5146 | return dli.dli_saddr; |
| 5147 | } |
| 5148 | else if (dli_main.dli_fbase != dli.dli_fbase) { |
| 5149 | /* unresolved symbol from a known library, report relative offset */ |
| 5150 | chunk_appendf(buf, "+%#lx", (long)(addr - dli.dli_fbase)); |
| 5151 | return NULL; |
| 5152 | } |
Baruch Siach | e1651b2 | 2020-07-24 07:52:20 +0300 | [diff] [blame] | 5153 | #endif /* __ELF__ && !__linux__ || USE_DL */ |
Willy Tarreau | eb8b1ca | 2020-03-03 17:09:08 +0100 | [diff] [blame] | 5154 | unknown: |
| 5155 | /* unresolved symbol from the main file, report relative offset to main */ |
| 5156 | if ((void*)addr < (void*)main) |
| 5157 | chunk_appendf(buf, "main-%#lx", (long)((void*)main - addr)); |
| 5158 | else |
| 5159 | chunk_appendf(buf, "main+%#lx", (long)(addr - (void*)main)); |
| 5160 | return NULL; |
Willy Tarreau | 0ebb511 | 2016-12-05 00:10:57 +0100 | [diff] [blame] | 5161 | } |
| 5162 | |
Willy Tarreau | 6ab7b21 | 2021-12-28 09:57:10 +0100 | [diff] [blame] | 5163 | /* On systems where this is supported, let's provide a possibility to enumerate |
| 5164 | * the list of object files. The output is appended to a buffer initialized by |
| 5165 | * the caller, with one name per line. A trailing zero is always emitted if data |
| 5166 | * are written. Only real objects are dumped (executable and .so libs). The |
| 5167 | * function returns non-zero if it dumps anything. These functions do not make |
| 5168 | * use of the trash so that it is possible for the caller to call them with the |
| 5169 | * trash on input. The output format may be platform-specific but at least one |
| 5170 | * version must emit raw object file names when argument is zero. |
| 5171 | */ |
| 5172 | #if defined(HA_HAVE_DUMP_LIBS) |
| 5173 | # if defined(HA_HAVE_DL_ITERATE_PHDR) |
| 5174 | /* the private <data> we pass below is a dump context initialized like this */ |
| 5175 | struct dl_dump_ctx { |
| 5176 | struct buffer *buf; |
| 5177 | int with_addr; |
| 5178 | }; |
| 5179 | |
| 5180 | static int dl_dump_libs_cb(struct dl_phdr_info *info, size_t size, void *data) |
| 5181 | { |
| 5182 | struct dl_dump_ctx *ctx = data; |
| 5183 | const char *fname; |
| 5184 | size_t p1, p2, beg, end; |
| 5185 | int idx; |
| 5186 | |
| 5187 | if (!info || !info->dlpi_name) |
| 5188 | goto leave; |
| 5189 | |
| 5190 | if (!*info->dlpi_name) |
| 5191 | fname = get_exec_path(); |
| 5192 | else if (strchr(info->dlpi_name, '/')) |
| 5193 | fname = info->dlpi_name; |
| 5194 | else |
| 5195 | /* else it's a VDSO or similar and we're not interested */ |
| 5196 | goto leave; |
| 5197 | |
| 5198 | if (!ctx->with_addr) |
| 5199 | goto dump_name; |
| 5200 | |
| 5201 | /* virtual addresses are relative to the load address and are per |
| 5202 | * pseudo-header, so we have to scan them all to find the furthest |
| 5203 | * one from the beginning. In this case we only dump entries if |
| 5204 | * they have at least one section. |
| 5205 | */ |
| 5206 | beg = ~0; end = 0; |
| 5207 | for (idx = 0; idx < info->dlpi_phnum; idx++) { |
| 5208 | if (!info->dlpi_phdr[idx].p_memsz) |
| 5209 | continue; |
| 5210 | p1 = info->dlpi_phdr[idx].p_vaddr; |
| 5211 | if (p1 < beg) |
| 5212 | beg = p1; |
| 5213 | p2 = p1 + info->dlpi_phdr[idx].p_memsz - 1; |
| 5214 | if (p2 > end) |
| 5215 | end = p2; |
| 5216 | } |
| 5217 | |
| 5218 | if (!idx) |
| 5219 | goto leave; |
| 5220 | |
| 5221 | chunk_appendf(ctx->buf, "0x%012llx-0x%012llx (0x%07llx) ", |
| 5222 | (ullong)info->dlpi_addr + beg, |
| 5223 | (ullong)info->dlpi_addr + end, |
| 5224 | (ullong)(end - beg + 1)); |
| 5225 | dump_name: |
| 5226 | chunk_appendf(ctx->buf, "%s\n", fname); |
| 5227 | leave: |
| 5228 | return 0; |
| 5229 | } |
| 5230 | |
| 5231 | /* dumps lib names and optionally address ranges */ |
| 5232 | int dump_libs(struct buffer *output, int with_addr) |
| 5233 | { |
| 5234 | struct dl_dump_ctx ctx = { .buf = output, .with_addr = with_addr }; |
| 5235 | size_t old_data = output->data; |
| 5236 | |
| 5237 | dl_iterate_phdr(dl_dump_libs_cb, &ctx); |
| 5238 | return output->data != old_data; |
| 5239 | } |
| 5240 | # else // no DL_ITERATE_PHDR |
| 5241 | # error "No dump_libs() function for this platform" |
| 5242 | # endif |
| 5243 | #else // no HA_HAVE_DUMP_LIBS |
| 5244 | |
| 5245 | /* unsupported platform: do not dump anything */ |
| 5246 | int dump_libs(struct buffer *output, int with_addr) |
| 5247 | { |
| 5248 | return 0; |
| 5249 | } |
| 5250 | |
| 5251 | #endif // HA_HAVE_DUMP_LIBS |
| 5252 | |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 5253 | /* |
| 5254 | * 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] | 5255 | * made of integer separated by dot characters. |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 5256 | * |
| 5257 | * First, initializes the value with <sz> as address to 0 and initializes the |
| 5258 | * array with <nums> as address to NULL. Then allocates the array with <nums> as |
| 5259 | * address updating <sz> pointed value to the size of this array. |
| 5260 | * |
| 5261 | * Returns 1 if succeeded, 0 if not. |
| 5262 | */ |
| 5263 | int parse_dotted_uints(const char *str, unsigned int **nums, size_t *sz) |
| 5264 | { |
| 5265 | unsigned int *n; |
| 5266 | const char *s, *end; |
| 5267 | |
| 5268 | s = str; |
| 5269 | *sz = 0; |
| 5270 | end = str + strlen(str); |
| 5271 | *nums = n = NULL; |
| 5272 | |
| 5273 | while (1) { |
| 5274 | unsigned int r; |
| 5275 | |
| 5276 | if (s >= end) |
| 5277 | break; |
| 5278 | |
| 5279 | r = read_uint(&s, end); |
| 5280 | /* Expected characters after having read an uint: '\0' or '.', |
| 5281 | * if '.', must not be terminal. |
| 5282 | */ |
Christopher Faulet | 4b52412 | 2021-02-11 10:42:41 +0100 | [diff] [blame] | 5283 | if (*s != '\0'&& (*s++ != '.' || s == end)) { |
| 5284 | free(n); |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 5285 | return 0; |
Christopher Faulet | 4b52412 | 2021-02-11 10:42:41 +0100 | [diff] [blame] | 5286 | } |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 5287 | |
Frédéric Lécaille | 12a7184 | 2019-02-26 18:19:48 +0100 | [diff] [blame] | 5288 | n = my_realloc2(n, (*sz + 1) * sizeof *n); |
Frédéric Lécaille | 3b71716 | 2019-02-25 15:04:22 +0100 | [diff] [blame] | 5289 | if (!n) |
| 5290 | return 0; |
| 5291 | |
| 5292 | n[(*sz)++] = r; |
| 5293 | } |
| 5294 | *nums = n; |
| 5295 | |
| 5296 | return 1; |
| 5297 | } |
| 5298 | |
Willy Tarreau | 4d589e7 | 2019-08-23 19:02:26 +0200 | [diff] [blame] | 5299 | |
| 5300 | /* returns the number of bytes needed to encode <v> as a varint. An inline |
| 5301 | * version exists for use with constants (__varint_bytes()). |
| 5302 | */ |
| 5303 | int varint_bytes(uint64_t v) |
| 5304 | { |
| 5305 | int len = 1; |
| 5306 | |
| 5307 | if (v >= 240) { |
| 5308 | v = (v - 240) >> 4; |
| 5309 | while (1) { |
| 5310 | len++; |
| 5311 | if (v < 128) |
| 5312 | break; |
| 5313 | v = (v - 128) >> 7; |
| 5314 | } |
| 5315 | } |
| 5316 | return len; |
| 5317 | } |
| 5318 | |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 5319 | |
| 5320 | /* Random number generator state, see below */ |
Willy Tarreau | 1544c14 | 2020-03-12 00:31:18 +0100 | [diff] [blame] | 5321 | static uint64_t ha_random_state[2] ALIGNED(2*sizeof(uint64_t)); |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 5322 | |
| 5323 | /* This is a thread-safe implementation of xoroshiro128** described below: |
| 5324 | * http://prng.di.unimi.it/ |
| 5325 | * It features a 2^128 long sequence, returns 64 high-quality bits on each call, |
| 5326 | * supports fast jumps and passes all common quality tests. It is thread-safe, |
| 5327 | * uses a double-cas on 64-bit architectures supporting it, and falls back to a |
| 5328 | * local lock on other ones. |
| 5329 | */ |
| 5330 | uint64_t ha_random64() |
| 5331 | { |
Willy Tarreau | 1544c14 | 2020-03-12 00:31:18 +0100 | [diff] [blame] | 5332 | uint64_t old[2] ALIGNED(2*sizeof(uint64_t)); |
| 5333 | uint64_t new[2] ALIGNED(2*sizeof(uint64_t)); |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 5334 | |
| 5335 | #if defined(USE_THREAD) && (!defined(HA_CAS_IS_8B) || !defined(HA_HAVE_CAS_DW)) |
| 5336 | static HA_SPINLOCK_T rand_lock; |
| 5337 | |
| 5338 | HA_SPIN_LOCK(OTHER_LOCK, &rand_lock); |
| 5339 | #endif |
| 5340 | |
| 5341 | old[0] = ha_random_state[0]; |
| 5342 | old[1] = ha_random_state[1]; |
| 5343 | |
| 5344 | #if defined(USE_THREAD) && defined(HA_CAS_IS_8B) && defined(HA_HAVE_CAS_DW) |
| 5345 | do { |
| 5346 | #endif |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 5347 | new[1] = old[0] ^ old[1]; |
| 5348 | new[0] = rotl64(old[0], 24) ^ new[1] ^ (new[1] << 16); // a, b |
| 5349 | new[1] = rotl64(new[1], 37); // c |
| 5350 | |
| 5351 | #if defined(USE_THREAD) && defined(HA_CAS_IS_8B) && defined(HA_HAVE_CAS_DW) |
| 5352 | } while (unlikely(!_HA_ATOMIC_DWCAS(ha_random_state, old, new))); |
| 5353 | #else |
| 5354 | ha_random_state[0] = new[0]; |
| 5355 | ha_random_state[1] = new[1]; |
| 5356 | #if defined(USE_THREAD) |
| 5357 | HA_SPIN_UNLOCK(OTHER_LOCK, &rand_lock); |
| 5358 | #endif |
| 5359 | #endif |
Willy Tarreau | b2475a1 | 2021-05-09 10:26:14 +0200 | [diff] [blame] | 5360 | return rotl64(old[0] * 5, 7) * 9; |
Willy Tarreau | 52bf839 | 2020-03-08 00:42:37 +0100 | [diff] [blame] | 5361 | } |
| 5362 | |
| 5363 | /* seeds the random state using up to <len> bytes from <seed>, starting with |
| 5364 | * the first non-zero byte. |
| 5365 | */ |
| 5366 | void ha_random_seed(const unsigned char *seed, size_t len) |
| 5367 | { |
| 5368 | size_t pos; |
| 5369 | |
| 5370 | /* the seed must not be all zeroes, so we pre-fill it with alternating |
| 5371 | * bits and overwrite part of them with the block starting at the first |
| 5372 | * non-zero byte from the seed. |
| 5373 | */ |
| 5374 | memset(ha_random_state, 0x55, sizeof(ha_random_state)); |
| 5375 | |
| 5376 | for (pos = 0; pos < len; pos++) |
| 5377 | if (seed[pos] != 0) |
| 5378 | break; |
| 5379 | |
| 5380 | if (pos == len) |
| 5381 | return; |
| 5382 | |
| 5383 | seed += pos; |
| 5384 | len -= pos; |
| 5385 | |
| 5386 | if (len > sizeof(ha_random_state)) |
| 5387 | len = sizeof(ha_random_state); |
| 5388 | |
| 5389 | memcpy(ha_random_state, seed, len); |
| 5390 | } |
| 5391 | |
| 5392 | /* This causes a jump to (dist * 2^96) places in the pseudo-random sequence, |
| 5393 | * and is equivalent to calling ha_random64() as many times. It is used to |
| 5394 | * provide non-overlapping sequences of 2^96 numbers (~7*10^28) to up to 2^32 |
| 5395 | * different generators (i.e. different processes after a fork). The <dist> |
| 5396 | * argument is the distance to jump to and is used in a loop so it rather not |
| 5397 | * be too large if the processing time is a concern. |
| 5398 | * |
| 5399 | * BEWARE: this function is NOT thread-safe and must not be called during |
| 5400 | * concurrent accesses to ha_random64(). |
| 5401 | */ |
| 5402 | void ha_random_jump96(uint32_t dist) |
| 5403 | { |
| 5404 | while (dist--) { |
| 5405 | uint64_t s0 = 0; |
| 5406 | uint64_t s1 = 0; |
| 5407 | int b; |
| 5408 | |
| 5409 | for (b = 0; b < 64; b++) { |
| 5410 | if ((0xd2a98b26625eee7bULL >> b) & 1) { |
| 5411 | s0 ^= ha_random_state[0]; |
| 5412 | s1 ^= ha_random_state[1]; |
| 5413 | } |
| 5414 | ha_random64(); |
| 5415 | } |
| 5416 | |
| 5417 | for (b = 0; b < 64; b++) { |
| 5418 | if ((0xdddf9b1090aa7ac1ULL >> b) & 1) { |
| 5419 | s0 ^= ha_random_state[0]; |
| 5420 | s1 ^= ha_random_state[1]; |
| 5421 | } |
| 5422 | ha_random64(); |
| 5423 | } |
| 5424 | ha_random_state[0] = s0; |
| 5425 | ha_random_state[1] = s1; |
| 5426 | } |
| 5427 | } |
| 5428 | |
Willy Tarreau | ee3bcdd | 2020-03-08 17:48:17 +0100 | [diff] [blame] | 5429 | /* Generates an RFC4122 UUID into chunk <output> which must be at least 37 |
| 5430 | * bytes large. |
| 5431 | */ |
| 5432 | void ha_generate_uuid(struct buffer *output) |
| 5433 | { |
| 5434 | uint32_t rnd[4]; |
| 5435 | uint64_t last; |
| 5436 | |
| 5437 | last = ha_random64(); |
| 5438 | rnd[0] = last; |
| 5439 | rnd[1] = last >> 32; |
| 5440 | |
| 5441 | last = ha_random64(); |
| 5442 | rnd[2] = last; |
| 5443 | rnd[3] = last >> 32; |
| 5444 | |
| 5445 | chunk_printf(output, "%8.8x-%4.4x-%4.4x-%4.4x-%12.12llx", |
| 5446 | rnd[0], |
| 5447 | rnd[1] & 0xFFFF, |
| 5448 | ((rnd[1] >> 16u) & 0xFFF) | 0x4000, // highest 4 bits indicate the uuid version |
| 5449 | (rnd[2] & 0x3FFF) | 0x8000, // the highest 2 bits indicate the UUID variant (10), |
| 5450 | (long long)((rnd[2] >> 14u) | ((uint64_t) rnd[3] << 18u)) & 0xFFFFFFFFFFFFull); |
| 5451 | } |
| 5452 | |
| 5453 | |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5454 | /* only used by parse_line() below. It supports writing in place provided that |
| 5455 | * <in> is updated to the next location before calling it. In that case, the |
| 5456 | * char at <in> may be overwritten. |
| 5457 | */ |
| 5458 | #define EMIT_CHAR(x) \ |
| 5459 | do { \ |
| 5460 | char __c = (char)(x); \ |
| 5461 | if ((opts & PARSE_OPT_INPLACE) && out+outpos > in) \ |
| 5462 | err |= PARSE_ERR_OVERLAP; \ |
| 5463 | if (outpos >= outmax) \ |
| 5464 | err |= PARSE_ERR_TOOLARGE; \ |
| 5465 | if (!err) \ |
| 5466 | out[outpos] = __c; \ |
| 5467 | outpos++; \ |
| 5468 | } while (0) |
| 5469 | |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 5470 | /* Parse <in>, copy it into <out> split into isolated words whose pointers |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5471 | * are put in <args>. If more than <outlen> bytes have to be emitted, the |
| 5472 | * extraneous ones are not emitted but <outlen> is updated so that the caller |
| 5473 | * knows how much to realloc. Similarly, <args> are not updated beyond <nbargs> |
| 5474 | * but the returned <nbargs> indicates how many were found. All trailing args |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5475 | * up to <nbargs> point to the trailing zero, and as long as <nbargs> is > 0, |
| 5476 | * it is guaranteed that at least one arg will point to the zero. It is safe |
| 5477 | * to call it with a NULL <args> if <nbargs> is 0. |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5478 | * |
| 5479 | * <out> may overlap with <in> provided that it never goes further, in which |
| 5480 | * case the parser will accept to perform in-place parsing and unquoting/ |
| 5481 | * unescaping but only if environment variables do not lead to expansion that |
| 5482 | * causes overlapping, otherwise the input string being destroyed, the error |
| 5483 | * will not be recoverable. Note that even during out-of-place <in> will |
| 5484 | * experience temporary modifications in-place for variable resolution and must |
| 5485 | * be writable, and will also receive zeroes to delimit words when using |
| 5486 | * in-place copy. Parsing options <opts> taken from PARSE_OPT_*. Return value |
| 5487 | * is zero on success otherwise a bitwise-or of PARSE_ERR_*. Upon error, the |
| 5488 | * starting point of the first invalid character sequence or unmatched |
| 5489 | * quote/brace is reported in <errptr> if not NULL. When using in-place parsing |
| 5490 | * error reporting might be difficult since zeroes will have been inserted into |
| 5491 | * the string. One solution for the caller may consist in replacing all args |
| 5492 | * delimiters with spaces in this case. |
| 5493 | */ |
Maximilian Mader | 29c6cd7 | 2021-06-06 00:50:21 +0200 | [diff] [blame] | 5494 | 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] | 5495 | { |
| 5496 | char *quote = NULL; |
| 5497 | char *brace = NULL; |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5498 | char *word_expand = NULL; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5499 | unsigned char hex1, hex2; |
| 5500 | size_t outmax = *outlen; |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5501 | int argsmax = *nbargs - 1; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5502 | size_t outpos = 0; |
| 5503 | int squote = 0; |
| 5504 | int dquote = 0; |
| 5505 | int arg = 0; |
| 5506 | uint32_t err = 0; |
| 5507 | |
| 5508 | *nbargs = 0; |
| 5509 | *outlen = 0; |
| 5510 | |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5511 | /* argsmax may be -1 here, protecting args[] from any write */ |
| 5512 | if (arg < argsmax) |
| 5513 | args[arg] = out; |
| 5514 | |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5515 | while (1) { |
| 5516 | if (*in >= '-' && *in != '\\') { |
| 5517 | /* speedup: directly send all regular chars starting |
| 5518 | * with '-', '.', '/', alnum etc... |
| 5519 | */ |
| 5520 | EMIT_CHAR(*in++); |
| 5521 | continue; |
| 5522 | } |
| 5523 | else if (*in == '\0' || *in == '\n' || *in == '\r') { |
| 5524 | /* end of line */ |
| 5525 | break; |
| 5526 | } |
| 5527 | else if (*in == '#' && (opts & PARSE_OPT_SHARP) && !squote && !dquote) { |
| 5528 | /* comment */ |
| 5529 | break; |
| 5530 | } |
| 5531 | else if (*in == '"' && !squote && (opts & PARSE_OPT_DQUOTE)) { /* double quote outside single quotes */ |
| 5532 | if (dquote) { |
| 5533 | dquote = 0; |
| 5534 | quote = NULL; |
| 5535 | } |
| 5536 | else { |
| 5537 | dquote = 1; |
| 5538 | quote = in; |
| 5539 | } |
| 5540 | in++; |
| 5541 | continue; |
| 5542 | } |
| 5543 | else if (*in == '\'' && !dquote && (opts & PARSE_OPT_SQUOTE)) { /* single quote outside double quotes */ |
| 5544 | if (squote) { |
| 5545 | squote = 0; |
| 5546 | quote = NULL; |
| 5547 | } |
| 5548 | else { |
| 5549 | squote = 1; |
| 5550 | quote = in; |
| 5551 | } |
| 5552 | in++; |
| 5553 | continue; |
| 5554 | } |
| 5555 | else if (*in == '\\' && !squote && (opts & PARSE_OPT_BKSLASH)) { |
| 5556 | /* first, we'll replace \\, \<space>, \#, \r, \n, \t, \xXX with their |
| 5557 | * C equivalent value but only when they have a special meaning and within |
| 5558 | * double quotes for some of them. Other combinations left unchanged (eg: \1). |
| 5559 | */ |
| 5560 | char tosend = *in; |
| 5561 | |
| 5562 | switch (in[1]) { |
| 5563 | case ' ': |
| 5564 | case '\\': |
| 5565 | tosend = in[1]; |
| 5566 | in++; |
| 5567 | break; |
| 5568 | |
| 5569 | case 't': |
| 5570 | tosend = '\t'; |
| 5571 | in++; |
| 5572 | break; |
| 5573 | |
| 5574 | case 'n': |
| 5575 | tosend = '\n'; |
| 5576 | in++; |
| 5577 | break; |
| 5578 | |
| 5579 | case 'r': |
| 5580 | tosend = '\r'; |
| 5581 | in++; |
| 5582 | break; |
| 5583 | |
| 5584 | case '#': |
| 5585 | /* escaping of "#" only if comments are supported */ |
| 5586 | if (opts & PARSE_OPT_SHARP) |
| 5587 | in++; |
| 5588 | tosend = *in; |
| 5589 | break; |
| 5590 | |
| 5591 | case '\'': |
| 5592 | /* escaping of "'" only outside single quotes and only if single quotes are supported */ |
| 5593 | if (opts & PARSE_OPT_SQUOTE && !squote) |
| 5594 | in++; |
| 5595 | tosend = *in; |
| 5596 | break; |
| 5597 | |
| 5598 | case '"': |
| 5599 | /* escaping of '"' only outside single quotes and only if double quotes are supported */ |
| 5600 | if (opts & PARSE_OPT_DQUOTE && !squote) |
| 5601 | in++; |
| 5602 | tosend = *in; |
| 5603 | break; |
| 5604 | |
| 5605 | case '$': |
| 5606 | /* escaping of '$' only inside double quotes and only if env supported */ |
| 5607 | if (opts & PARSE_OPT_ENV && dquote) |
| 5608 | in++; |
| 5609 | tosend = *in; |
| 5610 | break; |
| 5611 | |
| 5612 | case 'x': |
| 5613 | if (!ishex(in[2]) || !ishex(in[3])) { |
| 5614 | /* invalid or incomplete hex sequence */ |
| 5615 | err |= PARSE_ERR_HEX; |
| 5616 | if (errptr) |
| 5617 | *errptr = in; |
| 5618 | goto leave; |
| 5619 | } |
Willy Tarreau | f278eec | 2020-07-05 21:46:32 +0200 | [diff] [blame] | 5620 | hex1 = toupper((unsigned char)in[2]) - '0'; |
| 5621 | hex2 = toupper((unsigned char)in[3]) - '0'; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5622 | if (hex1 > 9) hex1 -= 'A' - '9' - 1; |
| 5623 | if (hex2 > 9) hex2 -= 'A' - '9' - 1; |
| 5624 | tosend = (hex1 << 4) + hex2; |
| 5625 | in += 3; |
| 5626 | break; |
| 5627 | |
| 5628 | default: |
| 5629 | /* other combinations are not escape sequences */ |
| 5630 | break; |
| 5631 | } |
| 5632 | |
| 5633 | in++; |
| 5634 | EMIT_CHAR(tosend); |
| 5635 | } |
| 5636 | else if (isspace((unsigned char)*in) && !squote && !dquote) { |
| 5637 | /* a non-escaped space is an argument separator */ |
| 5638 | while (isspace((unsigned char)*in)) |
| 5639 | in++; |
| 5640 | EMIT_CHAR(0); |
| 5641 | arg++; |
| 5642 | if (arg < argsmax) |
| 5643 | args[arg] = out + outpos; |
| 5644 | else |
| 5645 | err |= PARSE_ERR_TOOMANY; |
| 5646 | } |
| 5647 | else if (*in == '$' && (opts & PARSE_OPT_ENV) && (dquote || !(opts & PARSE_OPT_DQUOTE))) { |
| 5648 | /* environment variables are evaluated anywhere, or only |
| 5649 | * inside double quotes if they are supported. |
| 5650 | */ |
| 5651 | char *var_name; |
| 5652 | char save_char; |
Willy Tarreau | a46f1af | 2021-05-06 10:25:11 +0200 | [diff] [blame] | 5653 | const char *value; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5654 | |
| 5655 | in++; |
| 5656 | |
| 5657 | if (*in == '{') |
| 5658 | brace = in++; |
| 5659 | |
Willy Tarreau | a46f1af | 2021-05-06 10:25:11 +0200 | [diff] [blame] | 5660 | if (!isalpha((unsigned char)*in) && *in != '_' && *in != '.') { |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5661 | /* unacceptable character in variable name */ |
| 5662 | err |= PARSE_ERR_VARNAME; |
| 5663 | if (errptr) |
| 5664 | *errptr = in; |
| 5665 | goto leave; |
| 5666 | } |
| 5667 | |
| 5668 | var_name = in; |
Willy Tarreau | a46f1af | 2021-05-06 10:25:11 +0200 | [diff] [blame] | 5669 | if (*in == '.') |
| 5670 | in++; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5671 | while (isalnum((unsigned char)*in) || *in == '_') |
| 5672 | in++; |
| 5673 | |
| 5674 | save_char = *in; |
| 5675 | *in = '\0'; |
Willy Tarreau | a46f1af | 2021-05-06 10:25:11 +0200 | [diff] [blame] | 5676 | if (unlikely(*var_name == '.')) { |
| 5677 | /* internal pseudo-variables */ |
| 5678 | if (strcmp(var_name, ".LINE") == 0) |
| 5679 | value = ultoa(global.cfg_curr_line); |
| 5680 | else if (strcmp(var_name, ".FILE") == 0) |
| 5681 | value = global.cfg_curr_file; |
| 5682 | else if (strcmp(var_name, ".SECTION") == 0) |
| 5683 | value = global.cfg_curr_section; |
| 5684 | else { |
| 5685 | /* unsupported internal variable name */ |
| 5686 | err |= PARSE_ERR_VARNAME; |
| 5687 | if (errptr) |
| 5688 | *errptr = var_name; |
| 5689 | goto leave; |
| 5690 | } |
| 5691 | } else { |
| 5692 | value = getenv(var_name); |
| 5693 | } |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5694 | *in = save_char; |
| 5695 | |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5696 | /* support for '[*]' sequence to force word expansion, |
| 5697 | * only available inside braces */ |
| 5698 | if (*in == '[' && brace && (opts & PARSE_OPT_WORD_EXPAND)) { |
| 5699 | word_expand = in++; |
| 5700 | |
| 5701 | if (*in++ != '*' || *in++ != ']') { |
| 5702 | err |= PARSE_ERR_WRONG_EXPAND; |
| 5703 | if (errptr) |
| 5704 | *errptr = word_expand; |
| 5705 | goto leave; |
| 5706 | } |
| 5707 | } |
| 5708 | |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5709 | if (brace) { |
Willy Tarreau | ec347b1 | 2021-11-18 17:42:50 +0100 | [diff] [blame] | 5710 | if (*in == '-') { |
| 5711 | /* default value starts just after the '-' */ |
| 5712 | if (!value) |
| 5713 | value = in + 1; |
| 5714 | |
| 5715 | while (*in && *in != '}') |
| 5716 | in++; |
| 5717 | if (!*in) |
| 5718 | goto no_brace; |
| 5719 | *in = 0; // terminate the default value |
| 5720 | } |
| 5721 | else if (*in != '}') { |
| 5722 | no_brace: |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5723 | /* unmatched brace */ |
| 5724 | err |= PARSE_ERR_BRACE; |
| 5725 | if (errptr) |
| 5726 | *errptr = brace; |
| 5727 | goto leave; |
| 5728 | } |
Willy Tarreau | ec347b1 | 2021-11-18 17:42:50 +0100 | [diff] [blame] | 5729 | |
| 5730 | /* brace found, skip it */ |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5731 | in++; |
| 5732 | brace = NULL; |
| 5733 | } |
| 5734 | |
| 5735 | if (value) { |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5736 | while (*value) { |
| 5737 | /* expand as individual parameters on a space character */ |
Willy Tarreau | fe2cc41 | 2020-10-01 18:04:40 +0200 | [diff] [blame] | 5738 | if (word_expand && isspace((unsigned char)*value)) { |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5739 | EMIT_CHAR(0); |
| 5740 | ++arg; |
| 5741 | if (arg < argsmax) |
| 5742 | args[arg] = out + outpos; |
| 5743 | else |
| 5744 | err |= PARSE_ERR_TOOMANY; |
| 5745 | |
| 5746 | /* skip consecutive spaces */ |
Willy Tarreau | fe2cc41 | 2020-10-01 18:04:40 +0200 | [diff] [blame] | 5747 | while (isspace((unsigned char)*++value)) |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5748 | ; |
| 5749 | } else { |
| 5750 | EMIT_CHAR(*value++); |
| 5751 | } |
| 5752 | } |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5753 | } |
Amaury Denoyelle | fa41cb6 | 2020-10-01 14:32:35 +0200 | [diff] [blame] | 5754 | word_expand = NULL; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5755 | } |
| 5756 | else { |
| 5757 | /* any other regular char */ |
| 5758 | EMIT_CHAR(*in++); |
| 5759 | } |
| 5760 | } |
| 5761 | |
| 5762 | /* end of output string */ |
| 5763 | EMIT_CHAR(0); |
Erwan Le Goas | 8a6767d | 2022-09-23 15:06:34 +0200 | [diff] [blame] | 5764 | |
Willy Tarreau | 94ab139 | 2022-10-03 08:27:55 +0200 | [diff] [blame] | 5765 | /* Don't add an empty arg after trailing spaces. Note that args[arg] |
| 5766 | * may contain some distances relative to NULL if <out> was NULL, or |
| 5767 | * pointers beyond the end of <out> in case <outlen> is too short, thus |
| 5768 | * we must not dereference it. |
Erwan Le Goas | 8a6767d | 2022-09-23 15:06:34 +0200 | [diff] [blame] | 5769 | */ |
Willy Tarreau | 94ab139 | 2022-10-03 08:27:55 +0200 | [diff] [blame] | 5770 | if (arg < argsmax && args[arg] != out + outpos - 1) |
Erwan Le Goas | 8a6767d | 2022-09-23 15:06:34 +0200 | [diff] [blame] | 5771 | arg++; |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5772 | |
| 5773 | if (quote) { |
| 5774 | /* unmatched quote */ |
| 5775 | err |= PARSE_ERR_QUOTE; |
| 5776 | if (errptr) |
| 5777 | *errptr = quote; |
| 5778 | goto leave; |
| 5779 | } |
| 5780 | leave: |
| 5781 | *nbargs = arg; |
| 5782 | *outlen = outpos; |
| 5783 | |
Willy Tarreau | 61dd44b | 2020-06-25 07:35:42 +0200 | [diff] [blame] | 5784 | /* empty all trailing args by making them point to the trailing zero, |
| 5785 | * at least the last one in any case. |
| 5786 | */ |
| 5787 | if (arg > argsmax) |
| 5788 | arg = argsmax; |
| 5789 | |
| 5790 | while (arg >= 0 && arg <= argsmax) |
Willy Tarreau | c8d167b | 2020-06-16 16:27:26 +0200 | [diff] [blame] | 5791 | args[arg++] = out + outpos - 1; |
| 5792 | |
| 5793 | return err; |
| 5794 | } |
| 5795 | #undef EMIT_CHAR |
| 5796 | |
Willy Tarreau | c54e5ad | 2020-06-25 09:15:40 +0200 | [diff] [blame] | 5797 | /* This is used to sanitize an input line that's about to be used for error reporting. |
| 5798 | * It will adjust <line> to print approximately <width> chars around <pos>, trying to |
| 5799 | * preserve the beginning, with leading or trailing "..." when the line is truncated. |
| 5800 | * If non-printable chars are present in the output. It returns the new offset <pos> |
| 5801 | * in the modified line. Non-printable characters are replaced with '?'. <width> must |
| 5802 | * be at least 6 to support two "..." otherwise the result is undefined. The line |
| 5803 | * itself must have at least 7 chars allocated for the same reason. |
| 5804 | */ |
| 5805 | size_t sanitize_for_printing(char *line, size_t pos, size_t width) |
| 5806 | { |
| 5807 | size_t shift = 0; |
| 5808 | char *out = line; |
| 5809 | char *in = line; |
| 5810 | char *end = line + width; |
| 5811 | |
| 5812 | if (pos >= width) { |
| 5813 | /* if we have to shift, we'll be out of context, so let's |
| 5814 | * try to put <pos> at the center of width. |
| 5815 | */ |
| 5816 | shift = pos - width / 2; |
| 5817 | in += shift + 3; |
| 5818 | end = out + width - 3; |
| 5819 | out[0] = out[1] = out[2] = '.'; |
| 5820 | out += 3; |
| 5821 | } |
| 5822 | |
| 5823 | while (out < end && *in) { |
| 5824 | if (isspace((unsigned char)*in)) |
| 5825 | *out++ = ' '; |
| 5826 | else if (isprint((unsigned char)*in)) |
| 5827 | *out++ = *in; |
| 5828 | else |
| 5829 | *out++ = '?'; |
| 5830 | in++; |
| 5831 | } |
| 5832 | |
| 5833 | if (end < line + width) { |
| 5834 | out[0] = out[1] = out[2] = '.'; |
| 5835 | out += 3; |
| 5836 | } |
| 5837 | |
| 5838 | *out++ = 0; |
| 5839 | return pos - shift; |
| 5840 | } |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 5841 | |
Willy Tarreau | e33c4b3 | 2021-03-12 18:59:31 +0100 | [diff] [blame] | 5842 | /* Update array <fp> with the fingerprint of word <word> by counting the |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5843 | * transitions between characters. <fp> is a 1024-entries array indexed as |
| 5844 | * 32*from+to. Positions for 'from' and 'to' are: |
Willy Tarreau | 9294e88 | 2021-03-15 09:34:27 +0100 | [diff] [blame] | 5845 | * 1..26=letter, 27=digit, 28=other/begin/end. |
| 5846 | * 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] | 5847 | */ |
Willy Tarreau | e33c4b3 | 2021-03-12 18:59:31 +0100 | [diff] [blame] | 5848 | void update_word_fingerprint(uint8_t *fp, const char *word) |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5849 | { |
| 5850 | const char *p; |
| 5851 | int from, to; |
| 5852 | int c; |
| 5853 | |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5854 | from = 28; // begin |
| 5855 | for (p = word; *p; p++) { |
| 5856 | c = tolower(*p); |
| 5857 | switch(c) { |
Willy Tarreau | 9294e88 | 2021-03-15 09:34:27 +0100 | [diff] [blame] | 5858 | case 'a'...'z': to = c - 'a' + 1; break; |
| 5859 | case 'A'...'Z': to = tolower(c) - 'a' + 1; break; |
| 5860 | case '0'...'9': to = 27; break; |
| 5861 | default: to = 28; break; |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5862 | } |
Willy Tarreau | 9294e88 | 2021-03-15 09:34:27 +0100 | [diff] [blame] | 5863 | fp[to] = 1; |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5864 | fp[32 * from + to]++; |
| 5865 | from = to; |
| 5866 | } |
| 5867 | to = 28; // end |
| 5868 | fp[32 * from + to]++; |
| 5869 | } |
| 5870 | |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5871 | /* This function hashes a word, scramble is the anonymizing key, returns |
| 5872 | * the hashed word when the key (scramble) != 0, else returns the word. |
| 5873 | * This function can be called NB_L_HASH_WORD times in a row, don't call |
| 5874 | * it if you called it more than NB_L_HASH_WORD. |
| 5875 | */ |
| 5876 | const char *hash_anon(uint32_t scramble, const char *string2hash, const char *prefix, const char *suffix) |
| 5877 | { |
| 5878 | index_hash++; |
Erwan Le Goas | d2605cf | 2022-09-21 16:24:23 +0200 | [diff] [blame] | 5879 | if (index_hash == NB_L_HASH_WORD) |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5880 | index_hash = 0; |
| 5881 | |
| 5882 | /* don't hash empty strings */ |
| 5883 | if (!string2hash[0] || (string2hash[0] == ' ' && string2hash[1] == 0)) |
| 5884 | return string2hash; |
| 5885 | |
| 5886 | if (scramble != 0) { |
| 5887 | snprintf(hash_word[index_hash], sizeof(hash_word[index_hash]), "%s%06x%s", |
| 5888 | prefix, HA_ANON(scramble, string2hash, strlen(string2hash)), suffix); |
| 5889 | return hash_word[index_hash]; |
| 5890 | } |
| 5891 | else |
| 5892 | return string2hash; |
| 5893 | } |
| 5894 | |
| 5895 | /* This function hashes or not an ip address ipstring, scramble is the anonymizing |
| 5896 | * key, returns the hashed ip with his port or ipstring when there is nothing to hash. |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5897 | * Put hasport equal 0 to point out ipstring has no port, else put an other int. |
| 5898 | * Without port, return a simple hash or ipstring. |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5899 | */ |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5900 | const char *hash_ipanon(uint32_t scramble, char *ipstring, int hasport) |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5901 | { |
| 5902 | char *errmsg = NULL; |
| 5903 | struct sockaddr_storage *sa; |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5904 | struct sockaddr_storage ss; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5905 | char addr[46]; |
| 5906 | int port; |
| 5907 | |
| 5908 | index_hash++; |
Erwan Le Goas | d2605cf | 2022-09-21 16:24:23 +0200 | [diff] [blame] | 5909 | if (index_hash == NB_L_HASH_WORD) { |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5910 | index_hash = 0; |
| 5911 | } |
| 5912 | |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5913 | if (scramble == 0) { |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5914 | return ipstring; |
| 5915 | } |
Christopher Faulet | 015bbc2 | 2022-09-29 11:53:07 +0200 | [diff] [blame] | 5916 | if (strcmp(ipstring, "localhost") == 0 || |
| 5917 | strcmp(ipstring, "stdout") == 0 || |
| 5918 | strcmp(ipstring, "stderr") == 0 || |
| 5919 | strncmp(ipstring, "fd@", 3) == 0 || |
| 5920 | strncmp(ipstring, "sockpair@", 9) == 0) { |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5921 | return ipstring; |
| 5922 | } |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5923 | else { |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5924 | if (hasport == 0) { |
| 5925 | memset(&ss, 0, sizeof(ss)); |
| 5926 | if (str2ip2(ipstring, &ss, 1) == NULL) { |
| 5927 | return HA_ANON_STR(scramble, ipstring); |
| 5928 | } |
| 5929 | sa = &ss; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5930 | } |
| 5931 | else { |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5932 | sa = str2sa_range(ipstring, NULL, NULL, NULL, NULL, NULL, &errmsg, NULL, NULL, |
Christopher Faulet | 7e50e4b | 2022-09-29 11:46:34 +0200 | [diff] [blame] | 5933 | PA_O_PORT_OK | PA_O_STREAM | PA_O_DGRAM | PA_O_XPRT | PA_O_CONNECT | |
| 5934 | PA_O_PORT_RANGE | PA_O_PORT_OFS | PA_O_RESOLVE); |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5935 | if (sa == NULL) { |
| 5936 | return HA_ANON_STR(scramble, ipstring); |
| 5937 | } |
| 5938 | } |
| 5939 | addr_to_str(sa, addr, sizeof(addr)); |
| 5940 | port = get_host_port(sa); |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5941 | |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5942 | switch(sa->ss_family) { |
| 5943 | case AF_INET: |
| 5944 | if (strncmp(addr, "127", 3) == 0 || strncmp(addr, "255", 3) == 0 || strncmp(addr, "0", 1) == 0) { |
| 5945 | return ipstring; |
| 5946 | } |
| 5947 | else { |
| 5948 | if (port != 0) { |
| 5949 | snprintf(hash_word[index_hash], sizeof(hash_word[index_hash]), "IPV4(%06x):%d", HA_ANON(scramble, addr, strlen(addr)), port); |
| 5950 | return hash_word[index_hash]; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5951 | } |
| 5952 | else { |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5953 | snprintf(hash_word[index_hash], sizeof(hash_word[index_hash]), "IPV4(%06x)", HA_ANON(scramble, addr, strlen(addr))); |
| 5954 | return hash_word[index_hash]; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5955 | } |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5956 | } |
| 5957 | break; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5958 | |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5959 | case AF_INET6: |
| 5960 | if (strcmp(addr, "::1") == 0) { |
| 5961 | return ipstring; |
| 5962 | } |
| 5963 | else { |
| 5964 | if (port != 0) { |
| 5965 | snprintf(hash_word[index_hash], sizeof(hash_word[index_hash]), "IPV6(%06x):%d", HA_ANON(scramble, addr, strlen(addr)), port); |
| 5966 | return hash_word[index_hash]; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5967 | } |
| 5968 | else { |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5969 | snprintf(hash_word[index_hash], sizeof(hash_word[index_hash]), "IPV6(%06x)", HA_ANON(scramble, addr, strlen(addr))); |
| 5970 | return hash_word[index_hash]; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5971 | } |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5972 | } |
| 5973 | break; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5974 | |
Erwan Le Goas | 5eef158 | 2022-09-29 10:25:31 +0200 | [diff] [blame] | 5975 | case AF_UNIX: |
| 5976 | return HA_ANON_STR(scramble, ipstring); |
| 5977 | break; |
| 5978 | |
| 5979 | default: |
| 5980 | return ipstring; |
| 5981 | break; |
| 5982 | }; |
Erwan Le Goas | 9c76637 | 2022-09-14 17:40:24 +0200 | [diff] [blame] | 5983 | } |
| 5984 | return ipstring; |
| 5985 | } |
| 5986 | |
Willy Tarreau | e33c4b3 | 2021-03-12 18:59:31 +0100 | [diff] [blame] | 5987 | /* Initialize array <fp> with the fingerprint of word <word> by counting the |
| 5988 | * transitions between characters. <fp> is a 1024-entries array indexed as |
| 5989 | * 32*from+to. Positions for 'from' and 'to' are: |
| 5990 | * 0..25=letter, 26=digit, 27=other, 28=begin, 29=end, others unused. |
| 5991 | */ |
| 5992 | void make_word_fingerprint(uint8_t *fp, const char *word) |
| 5993 | { |
| 5994 | memset(fp, 0, 1024); |
| 5995 | update_word_fingerprint(fp, word); |
| 5996 | } |
| 5997 | |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 5998 | /* Return the distance between two word fingerprints created by function |
| 5999 | * 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] | 6000 | * the differences between each location. |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 6001 | */ |
| 6002 | int word_fingerprint_distance(const uint8_t *fp1, const uint8_t *fp2) |
| 6003 | { |
| 6004 | int i, k, dist = 0; |
| 6005 | |
| 6006 | for (i = 0; i < 1024; i++) { |
| 6007 | k = (int)fp1[i] - (int)fp2[i]; |
Willy Tarreau | 714c4c1 | 2021-03-15 09:44:53 +0100 | [diff] [blame] | 6008 | dist += abs(k); |
Willy Tarreau | ba2c445 | 2021-03-12 09:01:52 +0100 | [diff] [blame] | 6009 | } |
| 6010 | return dist; |
| 6011 | } |
| 6012 | |
William Lallemand | 3aeb3f9 | 2021-08-21 23:59:56 +0200 | [diff] [blame] | 6013 | /* |
| 6014 | * This function compares the loaded openssl version with a string <version> |
| 6015 | * This function use the same return code as compare_current_version: |
| 6016 | * |
| 6017 | * -1 : the version in argument is older than the current openssl version |
| 6018 | * 0 : the version in argument is the same as the current openssl version |
| 6019 | * 1 : the version in argument is newer than the current openssl version |
| 6020 | * |
| 6021 | * Or some errors: |
| 6022 | * -2 : openssl is not available on this process |
| 6023 | * -3 : the version in argument is not parsable |
| 6024 | */ |
| 6025 | int openssl_compare_current_version(const char *version) |
| 6026 | { |
| 6027 | #ifdef USE_OPENSSL |
| 6028 | int numversion; |
| 6029 | |
| 6030 | numversion = openssl_version_parser(version); |
| 6031 | if (numversion == 0) |
| 6032 | return -3; |
| 6033 | |
| 6034 | if (numversion < OPENSSL_VERSION_NUMBER) |
| 6035 | return -1; |
| 6036 | else if (numversion > OPENSSL_VERSION_NUMBER) |
| 6037 | return 1; |
| 6038 | else |
| 6039 | return 0; |
| 6040 | #else |
| 6041 | return -2; |
| 6042 | #endif |
| 6043 | } |
| 6044 | |
Remi Tricot-Le Breton | b01179a | 2021-10-11 15:34:12 +0200 | [diff] [blame] | 6045 | /* |
| 6046 | * This function compares the loaded openssl name with a string <name> |
| 6047 | * This function returns 0 if the OpenSSL name starts like the passed parameter, |
| 6048 | * 1 otherwise. |
| 6049 | */ |
| 6050 | int openssl_compare_current_name(const char *name) |
| 6051 | { |
| 6052 | #ifdef USE_OPENSSL |
| 6053 | int name_len = 0; |
| 6054 | const char *openssl_version = OpenSSL_version(OPENSSL_VERSION); |
| 6055 | |
| 6056 | if (name) { |
| 6057 | name_len = strlen(name); |
| 6058 | if (strlen(name) <= strlen(openssl_version)) |
| 6059 | return strncmp(openssl_version, name, name_len); |
| 6060 | } |
| 6061 | #endif |
| 6062 | return 1; |
| 6063 | } |
| 6064 | |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6065 | #if defined(RTLD_DEFAULT) || defined(RTLD_NEXT) |
| 6066 | /* redefine dlopen() so that we can detect unexpected replacement of some |
| 6067 | * critical symbols, typically init/alloc/free functions coming from alternate |
| 6068 | * libraries. When called, a tainted flag is set (TAINTED_SHARED_LIBS). |
| 6069 | */ |
| 6070 | void *dlopen(const char *filename, int flags) |
| 6071 | { |
| 6072 | static void *(*_dlopen)(const char *filename, int flags); |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6073 | struct { |
| 6074 | const char *name; |
| 6075 | void *curr, *next; |
| 6076 | } check_syms[] = { |
| 6077 | { .name = "malloc", }, |
| 6078 | { .name = "free", }, |
| 6079 | { .name = "SSL_library_init", }, |
| 6080 | { .name = "X509_free", }, |
| 6081 | /* insert only above, 0 must be the last one */ |
| 6082 | { 0 }, |
| 6083 | }; |
| 6084 | const char *trace; |
| 6085 | void *addr; |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6086 | void *ret; |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6087 | int sym = 0; |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6088 | |
| 6089 | if (!_dlopen) { |
| 6090 | _dlopen = get_sym_next_addr("dlopen"); |
| 6091 | if (!_dlopen || _dlopen == dlopen) { |
| 6092 | _dlopen = NULL; |
| 6093 | return NULL; |
| 6094 | } |
| 6095 | } |
| 6096 | |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6097 | /* save a few pointers to critical symbols. We keep a copy of both the |
| 6098 | * current and the next value, because we might already have replaced |
| 6099 | * some of them (e.g. malloc/free with DEBUG_MEM_STATS), and we're only |
| 6100 | * interested in verifying that a loaded library doesn't come with a |
| 6101 | * completely different definition that would be incompatible. |
| 6102 | */ |
| 6103 | for (sym = 0; check_syms[sym].name; sym++) { |
| 6104 | check_syms[sym].curr = get_sym_curr_addr(check_syms[sym].name); |
| 6105 | check_syms[sym].next = get_sym_next_addr(check_syms[sym].name); |
| 6106 | } |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6107 | |
| 6108 | /* now open the requested lib */ |
| 6109 | ret = _dlopen(filename, flags); |
| 6110 | if (!ret) |
| 6111 | return ret; |
| 6112 | |
| 6113 | mark_tainted(TAINTED_SHARED_LIBS); |
| 6114 | |
Willy Tarreau | 177aed5 | 2022-06-19 16:49:51 +0200 | [diff] [blame] | 6115 | /* and check that critical symbols didn't change */ |
| 6116 | for (sym = 0; check_syms[sym].name; sym++) { |
| 6117 | if (!check_syms[sym].curr && !check_syms[sym].next) |
| 6118 | continue; |
| 6119 | |
| 6120 | addr = dlsym(ret, check_syms[sym].name); |
| 6121 | if (!addr || addr == check_syms[sym].curr || addr == check_syms[sym].next) |
| 6122 | continue; |
| 6123 | |
| 6124 | /* OK it's clear that this symbol was redefined */ |
| 6125 | mark_tainted(TAINTED_REDEFINITION); |
| 6126 | |
| 6127 | trace = hlua_show_current_location("\n "); |
| 6128 | ha_warning("dlopen(): shared library '%s' brings a different definition of symbol '%s'. The process cannot be trusted anymore!%s%s\n", |
| 6129 | filename, check_syms[sym].name, |
| 6130 | trace ? " Suspected call location: \n " : "", |
| 6131 | trace ? trace : ""); |
| 6132 | } |
| 6133 | |
Willy Tarreau | 40dde2d | 2022-06-19 16:41:59 +0200 | [diff] [blame] | 6134 | return ret; |
| 6135 | } |
| 6136 | #endif |
| 6137 | |
Willy Tarreau | 06e69b5 | 2021-03-02 14:01:35 +0100 | [diff] [blame] | 6138 | static int init_tools_per_thread() |
| 6139 | { |
| 6140 | /* Let's make each thread start from a different position */ |
| 6141 | statistical_prng_state += tid * MAX_THREADS; |
| 6142 | if (!statistical_prng_state) |
| 6143 | statistical_prng_state++; |
| 6144 | return 1; |
| 6145 | } |
| 6146 | REGISTER_PER_THREAD_INIT(init_tools_per_thread); |
Willy Tarreau | c54e5ad | 2020-06-25 09:15:40 +0200 | [diff] [blame] | 6147 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 6148 | /* |
| 6149 | * Local variables: |
| 6150 | * c-indent-level: 8 |
| 6151 | * c-basic-offset: 8 |
| 6152 | * End: |
| 6153 | */ |