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