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