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