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