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