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