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