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