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> |
Willy Tarreau | 127f966 | 2007-12-06 00:53:51 +0100 | [diff] [blame] | 19 | #include <sys/socket.h> |
| 20 | #include <sys/un.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 21 | #include <netinet/in.h> |
| 22 | #include <arpa/inet.h> |
| 23 | |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 24 | #include <common/chunk.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 25 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 26 | #include <common/standard.h> |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 27 | #include <types/global.h> |
Willy Tarreau | 45cb4fb | 2009-10-26 21:10:04 +0100 | [diff] [blame] | 28 | #include <eb32tree.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 29 | |
Willy Tarreau | 56adcf2 | 2012-12-23 18:00:29 +0100 | [diff] [blame] | 30 | /* enough to store NB_ITOA_STR integers of : |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 31 | * 2^64-1 = 18446744073709551615 or |
| 32 | * -2^63 = -9223372036854775808 |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 33 | * |
| 34 | * The HTML version needs room for adding the 25 characters |
| 35 | * '<span class="rls"></span>' around digits at positions 3N+1 in order |
| 36 | * 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] | 37 | */ |
Willy Tarreau | 56adcf2 | 2012-12-23 18:00:29 +0100 | [diff] [blame] | 38 | char itoa_str[NB_ITOA_STR][171]; |
| 39 | int itoa_idx = 0; /* index of next itoa_str to use */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 40 | |
| 41 | /* |
William Lallemand | e7340ec | 2012-01-24 11:15:39 +0100 | [diff] [blame] | 42 | * unsigned long long ASCII representation |
| 43 | * |
| 44 | * return the last char '\0' or NULL if no enough |
| 45 | * space in dst |
| 46 | */ |
| 47 | char *ulltoa(unsigned long long n, char *dst, size_t size) |
| 48 | { |
| 49 | int i = 0; |
| 50 | char *res; |
| 51 | |
| 52 | switch(n) { |
| 53 | case 1ULL ... 9ULL: |
| 54 | i = 0; |
| 55 | break; |
| 56 | |
| 57 | case 10ULL ... 99ULL: |
| 58 | i = 1; |
| 59 | break; |
| 60 | |
| 61 | case 100ULL ... 999ULL: |
| 62 | i = 2; |
| 63 | break; |
| 64 | |
| 65 | case 1000ULL ... 9999ULL: |
| 66 | i = 3; |
| 67 | break; |
| 68 | |
| 69 | case 10000ULL ... 99999ULL: |
| 70 | i = 4; |
| 71 | break; |
| 72 | |
| 73 | case 100000ULL ... 999999ULL: |
| 74 | i = 5; |
| 75 | break; |
| 76 | |
| 77 | case 1000000ULL ... 9999999ULL: |
| 78 | i = 6; |
| 79 | break; |
| 80 | |
| 81 | case 10000000ULL ... 99999999ULL: |
| 82 | i = 7; |
| 83 | break; |
| 84 | |
| 85 | case 100000000ULL ... 999999999ULL: |
| 86 | i = 8; |
| 87 | break; |
| 88 | |
| 89 | case 1000000000ULL ... 9999999999ULL: |
| 90 | i = 9; |
| 91 | break; |
| 92 | |
| 93 | case 10000000000ULL ... 99999999999ULL: |
| 94 | i = 10; |
| 95 | break; |
| 96 | |
| 97 | case 100000000000ULL ... 999999999999ULL: |
| 98 | i = 11; |
| 99 | break; |
| 100 | |
| 101 | case 1000000000000ULL ... 9999999999999ULL: |
| 102 | i = 12; |
| 103 | break; |
| 104 | |
| 105 | case 10000000000000ULL ... 99999999999999ULL: |
| 106 | i = 13; |
| 107 | break; |
| 108 | |
| 109 | case 100000000000000ULL ... 999999999999999ULL: |
| 110 | i = 14; |
| 111 | break; |
| 112 | |
| 113 | case 1000000000000000ULL ... 9999999999999999ULL: |
| 114 | i = 15; |
| 115 | break; |
| 116 | |
| 117 | case 10000000000000000ULL ... 99999999999999999ULL: |
| 118 | i = 16; |
| 119 | break; |
| 120 | |
| 121 | case 100000000000000000ULL ... 999999999999999999ULL: |
| 122 | i = 17; |
| 123 | break; |
| 124 | |
| 125 | case 1000000000000000000ULL ... 9999999999999999999ULL: |
| 126 | i = 18; |
| 127 | break; |
| 128 | |
| 129 | case 10000000000000000000ULL ... ULLONG_MAX: |
| 130 | i = 19; |
| 131 | break; |
| 132 | } |
| 133 | if (i + 2 > size) // (i + 1) + '\0' |
| 134 | return NULL; // too long |
| 135 | res = dst + i + 1; |
| 136 | *res = '\0'; |
| 137 | for (; i >= 0; i--) { |
| 138 | dst[i] = n % 10ULL + '0'; |
| 139 | n /= 10ULL; |
| 140 | } |
| 141 | return res; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * unsigned long ASCII representation |
| 146 | * |
| 147 | * return the last char '\0' or NULL if no enough |
| 148 | * space in dst |
| 149 | */ |
| 150 | char *ultoa_o(unsigned long n, char *dst, size_t size) |
| 151 | { |
| 152 | int i = 0; |
| 153 | char *res; |
| 154 | |
| 155 | switch (n) { |
| 156 | case 0U ... 9UL: |
| 157 | i = 0; |
| 158 | break; |
| 159 | |
| 160 | case 10U ... 99UL: |
| 161 | i = 1; |
| 162 | break; |
| 163 | |
| 164 | case 100U ... 999UL: |
| 165 | i = 2; |
| 166 | break; |
| 167 | |
| 168 | case 1000U ... 9999UL: |
| 169 | i = 3; |
| 170 | break; |
| 171 | |
| 172 | case 10000U ... 99999UL: |
| 173 | i = 4; |
| 174 | break; |
| 175 | |
| 176 | case 100000U ... 999999UL: |
| 177 | i = 5; |
| 178 | break; |
| 179 | |
| 180 | case 1000000U ... 9999999UL: |
| 181 | i = 6; |
| 182 | break; |
| 183 | |
| 184 | case 10000000U ... 99999999UL: |
| 185 | i = 7; |
| 186 | break; |
| 187 | |
| 188 | case 100000000U ... 999999999UL: |
| 189 | i = 8; |
| 190 | break; |
| 191 | #if __WORDSIZE == 32 |
| 192 | |
| 193 | case 1000000000ULL ... ULONG_MAX: |
| 194 | i = 9; |
| 195 | break; |
| 196 | |
| 197 | #elif __WORDSIZE == 64 |
| 198 | |
| 199 | case 1000000000ULL ... 9999999999UL: |
| 200 | i = 9; |
| 201 | break; |
| 202 | |
| 203 | case 10000000000ULL ... 99999999999UL: |
| 204 | i = 10; |
| 205 | break; |
| 206 | |
| 207 | case 100000000000ULL ... 999999999999UL: |
| 208 | i = 11; |
| 209 | break; |
| 210 | |
| 211 | case 1000000000000ULL ... 9999999999999UL: |
| 212 | i = 12; |
| 213 | break; |
| 214 | |
| 215 | case 10000000000000ULL ... 99999999999999UL: |
| 216 | i = 13; |
| 217 | break; |
| 218 | |
| 219 | case 100000000000000ULL ... 999999999999999UL: |
| 220 | i = 14; |
| 221 | break; |
| 222 | |
| 223 | case 1000000000000000ULL ... 9999999999999999UL: |
| 224 | i = 15; |
| 225 | break; |
| 226 | |
| 227 | case 10000000000000000ULL ... 99999999999999999UL: |
| 228 | i = 16; |
| 229 | break; |
| 230 | |
| 231 | case 100000000000000000ULL ... 999999999999999999UL: |
| 232 | i = 17; |
| 233 | break; |
| 234 | |
| 235 | case 1000000000000000000ULL ... 9999999999999999999UL: |
| 236 | i = 18; |
| 237 | break; |
| 238 | |
| 239 | case 10000000000000000000ULL ... ULONG_MAX: |
| 240 | i = 19; |
| 241 | break; |
| 242 | |
| 243 | #endif |
| 244 | } |
| 245 | if (i + 2 > size) // (i + 1) + '\0' |
| 246 | return NULL; // too long |
| 247 | res = dst + i + 1; |
| 248 | *res = '\0'; |
| 249 | for (; i >= 0; i--) { |
| 250 | dst[i] = n % 10U + '0'; |
| 251 | n /= 10U; |
| 252 | } |
| 253 | return res; |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * signed long ASCII representation |
| 258 | * |
| 259 | * return the last char '\0' or NULL if no enough |
| 260 | * space in dst |
| 261 | */ |
| 262 | char *ltoa_o(long int n, char *dst, size_t size) |
| 263 | { |
| 264 | char *pos = dst; |
| 265 | |
| 266 | if (n < 0) { |
| 267 | if (size < 3) |
| 268 | return NULL; // min size is '-' + digit + '\0' but another test in ultoa |
| 269 | *pos = '-'; |
| 270 | pos++; |
| 271 | dst = ultoa_o(-n, pos, size - 1); |
| 272 | } else { |
| 273 | dst = ultoa_o(n, dst, size); |
| 274 | } |
| 275 | return dst; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * signed long long ASCII representation |
| 280 | * |
| 281 | * return the last char '\0' or NULL if no enough |
| 282 | * space in dst |
| 283 | */ |
| 284 | char *lltoa(long long n, char *dst, size_t size) |
| 285 | { |
| 286 | char *pos = dst; |
| 287 | |
| 288 | if (n < 0) { |
| 289 | if (size < 3) |
| 290 | return NULL; // min size is '-' + digit + '\0' but another test in ulltoa |
| 291 | *pos = '-'; |
| 292 | pos++; |
| 293 | dst = ulltoa(-n, pos, size - 1); |
| 294 | } else { |
| 295 | dst = ulltoa(n, dst, size); |
| 296 | } |
| 297 | return dst; |
| 298 | } |
| 299 | |
| 300 | /* |
| 301 | * write a ascii representation of a unsigned into dst, |
| 302 | * return a pointer to the last character |
| 303 | * Pad the ascii representation with '0', using size. |
| 304 | */ |
| 305 | char *utoa_pad(unsigned int n, char *dst, size_t size) |
| 306 | { |
| 307 | int i = 0; |
| 308 | char *ret; |
| 309 | |
| 310 | switch(n) { |
| 311 | case 0U ... 9U: |
| 312 | i = 0; |
| 313 | break; |
| 314 | |
| 315 | case 10U ... 99U: |
| 316 | i = 1; |
| 317 | break; |
| 318 | |
| 319 | case 100U ... 999U: |
| 320 | i = 2; |
| 321 | break; |
| 322 | |
| 323 | case 1000U ... 9999U: |
| 324 | i = 3; |
| 325 | break; |
| 326 | |
| 327 | case 10000U ... 99999U: |
| 328 | i = 4; |
| 329 | break; |
| 330 | |
| 331 | case 100000U ... 999999U: |
| 332 | i = 5; |
| 333 | break; |
| 334 | |
| 335 | case 1000000U ... 9999999U: |
| 336 | i = 6; |
| 337 | break; |
| 338 | |
| 339 | case 10000000U ... 99999999U: |
| 340 | i = 7; |
| 341 | break; |
| 342 | |
| 343 | case 100000000U ... 999999999U: |
| 344 | i = 8; |
| 345 | break; |
| 346 | |
| 347 | case 1000000000U ... 4294967295U: |
| 348 | i = 9; |
| 349 | break; |
| 350 | } |
| 351 | if (i + 2 > size) // (i + 1) + '\0' |
| 352 | return NULL; // too long |
| 353 | if (i < size) |
| 354 | i = size - 2; // padding - '\0' |
| 355 | |
| 356 | ret = dst + i + 1; |
| 357 | *ret = '\0'; |
| 358 | for (; i >= 0; i--) { |
| 359 | dst[i] = n % 10U + '0'; |
| 360 | n /= 10U; |
| 361 | } |
| 362 | return ret; |
| 363 | } |
| 364 | |
| 365 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 366 | * copies at most <size-1> chars from <src> to <dst>. Last char is always |
| 367 | * set to 0, unless <size> is 0. The number of chars copied is returned |
| 368 | * (excluding the terminating zero). |
| 369 | * This code has been optimized for size and speed : on x86, it's 45 bytes |
| 370 | * long, uses only registers, and consumes only 4 cycles per char. |
| 371 | */ |
| 372 | int strlcpy2(char *dst, const char *src, int size) |
| 373 | { |
| 374 | char *orig = dst; |
| 375 | if (size) { |
| 376 | while (--size && (*dst = *src)) { |
| 377 | src++; dst++; |
| 378 | } |
| 379 | *dst = 0; |
| 380 | } |
| 381 | return dst - orig; |
| 382 | } |
| 383 | |
| 384 | /* |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 385 | * This function simply returns a locally allocated string containing |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 386 | * the ascii representation for number 'n' in decimal. |
| 387 | */ |
Emeric Brun | 3a7fce5 | 2010-01-04 14:54:38 +0100 | [diff] [blame] | 388 | char *ultoa_r(unsigned long n, char *buffer, int size) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 389 | { |
| 390 | char *pos; |
| 391 | |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 392 | pos = buffer + size - 1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 393 | *pos-- = '\0'; |
| 394 | |
| 395 | do { |
| 396 | *pos-- = '0' + n % 10; |
| 397 | n /= 10; |
Willy Tarreau | 72d759c | 2007-10-25 12:14:10 +0200 | [diff] [blame] | 398 | } while (n && pos >= buffer); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 399 | return pos + 1; |
| 400 | } |
| 401 | |
Willy Tarreau | 91092e5 | 2007-10-25 16:58:42 +0200 | [diff] [blame] | 402 | /* |
Willy Tarreau | e7239b5 | 2009-03-29 13:41:58 +0200 | [diff] [blame] | 403 | * This function simply returns a locally allocated string containing |
| 404 | * the ascii representation for number 'n' in decimal, formatted for |
| 405 | * HTML output with tags to create visual grouping by 3 digits. The |
| 406 | * output needs to support at least 171 characters. |
| 407 | */ |
| 408 | const char *ulltoh_r(unsigned long long n, char *buffer, int size) |
| 409 | { |
| 410 | char *start; |
| 411 | int digit = 0; |
| 412 | |
| 413 | start = buffer + size; |
| 414 | *--start = '\0'; |
| 415 | |
| 416 | do { |
| 417 | if (digit == 3 && start >= buffer + 7) |
| 418 | memcpy(start -= 7, "</span>", 7); |
| 419 | |
| 420 | if (start >= buffer + 1) { |
| 421 | *--start = '0' + n % 10; |
| 422 | n /= 10; |
| 423 | } |
| 424 | |
| 425 | if (digit == 3 && start >= buffer + 18) |
| 426 | memcpy(start -= 18, "<span class=\"rls\">", 18); |
| 427 | |
| 428 | if (digit++ == 3) |
| 429 | digit = 1; |
| 430 | } while (n && start > buffer); |
| 431 | return start; |
| 432 | } |
| 433 | |
| 434 | /* |
Willy Tarreau | 91092e5 | 2007-10-25 16:58:42 +0200 | [diff] [blame] | 435 | * This function simply returns a locally allocated string containing the ascii |
| 436 | * representation for number 'n' in decimal, unless n is 0 in which case it |
| 437 | * returns the alternate string (or an empty string if the alternate string is |
| 438 | * NULL). It use is intended for limits reported in reports, where it's |
| 439 | * desirable not to display anything if there is no limit. Warning! it shares |
| 440 | * the same vector as ultoa_r(). |
| 441 | */ |
| 442 | const char *limit_r(unsigned long n, char *buffer, int size, const char *alt) |
| 443 | { |
| 444 | return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : ""); |
| 445 | } |
| 446 | |
Robert Tsai | 81ae195 | 2007-12-05 10:47:29 +0100 | [diff] [blame] | 447 | /* |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 448 | * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero. |
| 449 | * |
| 450 | * It looks like this one would be a good candidate for inlining, but this is |
| 451 | * not interesting because it around 35 bytes long and often called multiple |
| 452 | * times within the same function. |
| 453 | */ |
| 454 | int ishex(char s) |
| 455 | { |
| 456 | s -= '0'; |
| 457 | if ((unsigned char)s <= 9) |
| 458 | return 1; |
| 459 | s -= 'A' - '0'; |
| 460 | if ((unsigned char)s <= 5) |
| 461 | return 1; |
| 462 | s -= 'a' - 'A'; |
| 463 | if ((unsigned char)s <= 5) |
| 464 | return 1; |
| 465 | return 0; |
| 466 | } |
| 467 | |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 468 | /* |
| 469 | * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an |
| 470 | * invalid character is found, a pointer to it is returned. If everything is |
| 471 | * fine, NULL is returned. |
| 472 | */ |
| 473 | const char *invalid_char(const char *name) |
| 474 | { |
| 475 | if (!*name) |
| 476 | return name; |
| 477 | |
| 478 | while (*name) { |
Willy Tarreau | 88e0581 | 2010-03-03 00:16:00 +0100 | [diff] [blame] | 479 | if (!isalnum((int)(unsigned char)*name) && *name != '.' && *name != ':' && |
Willy Tarreau | 2e74c3f | 2007-12-02 18:45:09 +0100 | [diff] [blame] | 480 | *name != '_' && *name != '-') |
| 481 | return name; |
| 482 | name++; |
| 483 | } |
| 484 | return NULL; |
| 485 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 486 | |
| 487 | /* |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 488 | * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-]. |
| 489 | * If an invalid character is found, a pointer to it is returned. |
| 490 | * If everything is fine, NULL is returned. |
| 491 | */ |
| 492 | const char *invalid_domainchar(const char *name) { |
| 493 | |
| 494 | if (!*name) |
| 495 | return name; |
| 496 | |
| 497 | while (*name) { |
Willy Tarreau | 88e0581 | 2010-03-03 00:16:00 +0100 | [diff] [blame] | 498 | if (!isalnum((int)(unsigned char)*name) && *name != '.' && |
Krzysztof Piotr Oledzki | efe3b6f | 2008-05-23 23:49:32 +0200 | [diff] [blame] | 499 | *name != '_' && *name != '-') |
| 500 | return name; |
| 501 | |
| 502 | name++; |
| 503 | } |
| 504 | |
| 505 | return NULL; |
| 506 | } |
| 507 | |
| 508 | /* |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 509 | * converts <str> to a struct sockaddr_storage* provided by the caller. The |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 510 | * caller must have zeroed <sa> first, and may have set sa->ss_family to force |
| 511 | * parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then |
| 512 | * the function tries to guess the address family from the syntax. If the |
| 513 | * 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] | 514 | * string is assumed to contain only an address, no port. The address can be a |
| 515 | * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to |
| 516 | * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved. |
| 517 | * The return address will only have the address family and the address set, |
| 518 | * all other fields remain zero. The string is not supposed to be modified. |
| 519 | * The IPv6 '::' address is IN6ADDR_ANY. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 520 | */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 521 | static struct sockaddr_storage *str2ip(const char *str, struct sockaddr_storage *sa) |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 522 | { |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 523 | struct hostent *he; |
| 524 | |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 525 | /* Any IPv6 address */ |
| 526 | if (str[0] == ':' && str[1] == ':' && !str[2]) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 527 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 528 | sa->ss_family = AF_INET6; |
| 529 | else if (sa->ss_family != AF_INET6) |
| 530 | goto fail; |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 531 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 532 | } |
| 533 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 534 | /* Any address for the family, defaults to IPv4 */ |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 535 | if (!str[0] || (str[0] == '*' && !str[1])) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 536 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 537 | sa->ss_family = AF_INET; |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 538 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 539 | } |
| 540 | |
| 541 | /* check for IPv6 first */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 542 | if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET6) && |
| 543 | inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)sa)->sin6_addr)) { |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 544 | sa->ss_family = AF_INET6; |
| 545 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 546 | } |
| 547 | |
| 548 | /* then check for IPv4 */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 549 | if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET) && |
| 550 | inet_pton(AF_INET, str, &((struct sockaddr_in *)sa)->sin_addr)) { |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 551 | sa->ss_family = AF_INET; |
| 552 | return sa; |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 553 | } |
| 554 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 555 | #ifdef USE_GETADDRINFO |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 556 | if (global.tune.options & GTUNE_USE_GAI) { |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 557 | struct addrinfo hints, *result; |
| 558 | |
| 559 | memset(&result, 0, sizeof(result)); |
| 560 | memset(&hints, 0, sizeof(hints)); |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 561 | hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 562 | hints.ai_socktype = SOCK_DGRAM; |
| 563 | hints.ai_flags = AI_PASSIVE; |
| 564 | hints.ai_protocol = 0; |
| 565 | |
| 566 | if (getaddrinfo(str, NULL, &hints, &result) == 0) { |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 567 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 568 | sa->ss_family = result->ai_family; |
| 569 | else if (sa->ss_family != result->ai_family) |
| 570 | goto fail; |
| 571 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 572 | switch (result->ai_family) { |
| 573 | case AF_INET: |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 574 | memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen); |
| 575 | return sa; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 576 | case AF_INET6: |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 577 | memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen); |
| 578 | return sa; |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 579 | } |
| 580 | } |
| 581 | |
Sean Carey | 58ea039 | 2013-02-15 23:39:18 +0100 | [diff] [blame] | 582 | if (result) |
| 583 | freeaddrinfo(result); |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 584 | } |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 585 | #endif |
Nenad Merdanovic | 88afe03 | 2014-04-14 15:56:58 +0200 | [diff] [blame] | 586 | /* try to resolve an IPv4/IPv6 hostname */ |
| 587 | he = gethostbyname(str); |
| 588 | if (he) { |
| 589 | if (!sa->ss_family || sa->ss_family == AF_UNSPEC) |
| 590 | sa->ss_family = he->h_addrtype; |
| 591 | else if (sa->ss_family != he->h_addrtype) |
| 592 | goto fail; |
| 593 | |
| 594 | switch (sa->ss_family) { |
| 595 | case AF_INET: |
| 596 | ((struct sockaddr_in *)sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 597 | return sa; |
| 598 | case AF_INET6: |
| 599 | ((struct sockaddr_in6 *)sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list); |
| 600 | return sa; |
| 601 | } |
| 602 | } |
| 603 | |
David du Colombier | d5f4328 | 2011-03-17 10:40:16 +0100 | [diff] [blame] | 604 | /* unsupported address family */ |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 605 | fail: |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 606 | return NULL; |
| 607 | } |
| 608 | |
| 609 | /* |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 610 | * Converts <str> to a locally allocated struct sockaddr_storage *, and a port |
| 611 | * range or offset consisting in two integers that the caller will have to |
| 612 | * check to find the relevant input format. The following format are supported : |
| 613 | * |
| 614 | * String format | address | port | low | high |
| 615 | * addr | <addr> | 0 | 0 | 0 |
| 616 | * addr: | <addr> | 0 | 0 | 0 |
| 617 | * addr:port | <addr> | <port> | <port> | <port> |
| 618 | * addr:pl-ph | <addr> | <pl> | <pl> | <ph> |
| 619 | * addr:+port | <addr> | <port> | 0 | <port> |
| 620 | * addr:-port | <addr> |-<port> | <port> | 0 |
| 621 | * |
| 622 | * The detection of a port range or increment by the caller is made by |
| 623 | * comparing <low> and <high>. If both are equal, then port 0 means no port |
| 624 | * was specified. The caller may pass NULL for <low> and <high> if it is not |
| 625 | * interested in retrieving port ranges. |
| 626 | * |
| 627 | * Note that <addr> above may also be : |
| 628 | * - empty ("") => family will be AF_INET and address will be INADDR_ANY |
| 629 | * - "*" => family will be AF_INET and address will be INADDR_ANY |
| 630 | * - "::" => family will be AF_INET6 and address will be IN6ADDR_ANY |
| 631 | * - a host name => family and address will depend on host name resolving. |
| 632 | * |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 633 | * A prefix may be passed in before the address above to force the family : |
| 634 | * - "ipv4@" => force address to resolve as IPv4 and fail if not possible. |
| 635 | * - "ipv6@" => force address to resolve as IPv6 and fail if not possible. |
| 636 | * - "unix@" => force address to be a path to a UNIX socket even if the |
| 637 | * path does not start with a '/' |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 638 | * - "fd@" => an integer must follow, and is a file descriptor number. |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 639 | * |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 640 | * Also note that in order to avoid any ambiguity with IPv6 addresses, the ':' |
| 641 | * is mandatory after the IP address even when no port is specified. NULL is |
| 642 | * returned if the address cannot be parsed. The <low> and <high> ports are |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 643 | * always initialized if non-null, even for non-IP families. |
Willy Tarreau | d393a62 | 2013-03-04 18:22:00 +0100 | [diff] [blame] | 644 | * |
| 645 | * If <pfx> is non-null, it is used as a string prefix before any path-based |
| 646 | * address (typically the path to a unix socket). |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 647 | * |
| 648 | * When a file descriptor is passed, its value is put into the s_addr part of |
| 649 | * 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] | 650 | */ |
Willy Tarreau | d393a62 | 2013-03-04 18:22:00 +0100 | [diff] [blame] | 651 | struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high, char **err, const char *pfx) |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 652 | { |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 653 | static struct sockaddr_storage ss; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 654 | struct sockaddr_storage *ret = NULL; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 655 | char *back, *str2; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 656 | char *port1, *port2; |
| 657 | int portl, porth, porta; |
| 658 | |
| 659 | portl = porth = porta = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 660 | |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 661 | str2 = back = env_expand(strdup(str)); |
Willy Tarreau | df350f1 | 2013-03-01 20:22:54 +0100 | [diff] [blame] | 662 | if (str2 == NULL) { |
| 663 | memprintf(err, "out of memory in '%s'\n", __FUNCTION__); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 664 | goto out; |
Willy Tarreau | df350f1 | 2013-03-01 20:22:54 +0100 | [diff] [blame] | 665 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 666 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 667 | memset(&ss, 0, sizeof(ss)); |
| 668 | |
| 669 | if (strncmp(str2, "unix@", 5) == 0) { |
| 670 | str2 += 5; |
| 671 | ss.ss_family = AF_UNIX; |
| 672 | } |
| 673 | else if (strncmp(str2, "ipv4@", 5) == 0) { |
| 674 | str2 += 5; |
| 675 | ss.ss_family = AF_INET; |
| 676 | } |
| 677 | else if (strncmp(str2, "ipv6@", 5) == 0) { |
| 678 | str2 += 5; |
| 679 | ss.ss_family = AF_INET6; |
| 680 | } |
| 681 | else if (*str2 == '/') { |
| 682 | ss.ss_family = AF_UNIX; |
| 683 | } |
| 684 | else |
| 685 | ss.ss_family = AF_UNSPEC; |
| 686 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 687 | if (ss.ss_family == AF_UNSPEC && strncmp(str2, "fd@", 3) == 0) { |
| 688 | char *endptr; |
| 689 | |
| 690 | str2 += 3; |
| 691 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = strtol(str2, &endptr, 10); |
| 692 | |
| 693 | if (!*str2 || *endptr) { |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 694 | 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] | 695 | goto out; |
| 696 | } |
| 697 | |
| 698 | /* we return AF_UNSPEC if we use a file descriptor number */ |
| 699 | ss.ss_family = AF_UNSPEC; |
| 700 | } |
| 701 | else if (ss.ss_family == AF_UNIX) { |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 702 | int prefix_path_len; |
| 703 | int max_path_len; |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 704 | int adr_len; |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 705 | |
| 706 | /* complete unix socket path name during startup or soft-restart is |
| 707 | * <unix_bind_prefix><path>.<pid>.<bak|tmp> |
| 708 | */ |
| 709 | prefix_path_len = pfx ? strlen(pfx) : 0; |
| 710 | max_path_len = (sizeof(((struct sockaddr_un *)&ss)->sun_path) - 1) - |
| 711 | (prefix_path_len ? prefix_path_len + 1 + 5 + 1 + 3 : 0); |
| 712 | |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 713 | adr_len = strlen(str2); |
| 714 | if (adr_len > max_path_len) { |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 715 | memprintf(err, "socket path '%s' too long (max %d)\n", str, max_path_len); |
| 716 | goto out; |
| 717 | } |
| 718 | |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 719 | if (prefix_path_len) |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 720 | memcpy(((struct sockaddr_un *)&ss)->sun_path, pfx, prefix_path_len); |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 721 | memcpy(((struct sockaddr_un *)&ss)->sun_path + prefix_path_len, str2, adr_len + 1); |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 722 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 723 | else { /* IPv4 and IPv6 */ |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 724 | port1 = strrchr(str2, ':'); |
| 725 | if (port1) |
| 726 | *port1++ = '\0'; |
| 727 | else |
| 728 | port1 = ""; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 729 | |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 730 | if (str2ip(str2, &ss) == NULL) { |
| 731 | memprintf(err, "invalid address: '%s' in '%s'\n", str2, str); |
| 732 | goto out; |
| 733 | } |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 734 | |
Willy Tarreau | a39d199 | 2013-04-01 20:37:42 +0200 | [diff] [blame] | 735 | if (isdigit((int)(unsigned char)*port1)) { /* single port or range */ |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 736 | port2 = strchr(port1, '-'); |
| 737 | if (port2) |
| 738 | *port2++ = '\0'; |
| 739 | else |
| 740 | port2 = port1; |
| 741 | portl = atoi(port1); |
| 742 | porth = atoi(port2); |
| 743 | porta = portl; |
| 744 | } |
| 745 | else if (*port1 == '-') { /* negative offset */ |
| 746 | portl = atoi(port1 + 1); |
| 747 | porta = -portl; |
| 748 | } |
| 749 | else if (*port1 == '+') { /* positive offset */ |
| 750 | porth = atoi(port1 + 1); |
| 751 | porta = porth; |
| 752 | } |
| 753 | else if (*port1) { /* other any unexpected char */ |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 754 | 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] | 755 | goto out; |
| 756 | } |
| 757 | set_host_port(&ss, porta); |
Willy Tarreau | e4c58c8 | 2013-03-06 15:28:17 +0100 | [diff] [blame] | 758 | } |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 759 | |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 760 | ret = &ss; |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 761 | out: |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 762 | if (low) |
| 763 | *low = portl; |
| 764 | if (high) |
| 765 | *high = porth; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 766 | free(back); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 767 | return ret; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 768 | } |
| 769 | |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 770 | /* converts <str> to a struct in_addr containing a network mask. It can be |
| 771 | * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1 |
| 772 | * if the conversion succeeds otherwise non-zero. |
| 773 | */ |
| 774 | int str2mask(const char *str, struct in_addr *mask) |
| 775 | { |
| 776 | if (strchr(str, '.') != NULL) { /* dotted notation */ |
| 777 | if (!inet_pton(AF_INET, str, mask)) |
| 778 | return 0; |
| 779 | } |
| 780 | else { /* mask length */ |
| 781 | char *err; |
| 782 | unsigned long len = strtol(str, &err, 10); |
| 783 | |
| 784 | if (!*str || (err && *err) || (unsigned)len > 32) |
| 785 | return 0; |
| 786 | if (len) |
| 787 | mask->s_addr = htonl(~0UL << (32 - len)); |
| 788 | else |
| 789 | mask->s_addr = 0; |
| 790 | } |
| 791 | return 1; |
| 792 | } |
| 793 | |
Thierry FOURNIER | b050463 | 2013-12-14 15:39:02 +0100 | [diff] [blame] | 794 | /* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion |
| 795 | * succeeds otherwise zero. |
| 796 | */ |
| 797 | int cidr2dotted(int cidr, struct in_addr *mask) { |
| 798 | |
| 799 | if (cidr < 0 || cidr > 32) |
| 800 | return 0; |
| 801 | |
| 802 | mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0; |
| 803 | return 1; |
| 804 | } |
| 805 | |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 806 | /* |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 807 | * converts <str> to two struct in_addr* which must be pre-allocated. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 808 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 809 | * is optionnal and either in the dotted or CIDR notation. |
| 810 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 811 | */ |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 812 | 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] | 813 | { |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 814 | __label__ out_free, out_err; |
| 815 | char *c, *s; |
| 816 | int ret_val; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 817 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 818 | s = strdup(str); |
| 819 | if (!s) |
| 820 | return 0; |
| 821 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 822 | memset(mask, 0, sizeof(*mask)); |
| 823 | memset(addr, 0, sizeof(*addr)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 824 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 825 | if ((c = strrchr(s, '/')) != NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 826 | *c++ = '\0'; |
| 827 | /* c points to the mask */ |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 828 | if (!str2mask(c, mask)) |
| 829 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 830 | } |
| 831 | else { |
Willy Tarreau | ebd6160 | 2006-12-30 11:54:15 +0100 | [diff] [blame] | 832 | mask->s_addr = ~0U; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 833 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 834 | if (!inet_pton(AF_INET, s, addr)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 835 | struct hostent *he; |
| 836 | |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 837 | if (!resolve) |
| 838 | goto out_err; |
| 839 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 840 | if ((he = gethostbyname(s)) == NULL) { |
| 841 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 842 | } |
| 843 | else |
| 844 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 845 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 846 | |
| 847 | ret_val = 1; |
| 848 | out_free: |
| 849 | free(s); |
| 850 | return ret_val; |
| 851 | out_err: |
| 852 | ret_val = 0; |
| 853 | goto out_free; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 854 | } |
| 855 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 856 | |
| 857 | /* |
Willy Tarreau | 6d20e28 | 2012-04-27 22:49:47 +0200 | [diff] [blame] | 858 | * converts <str> to two struct in6_addr* which must be pre-allocated. |
| 859 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 860 | * is an optionnal number of bits (128 being the default). |
| 861 | * Returns 1 if OK, 0 if error. |
| 862 | */ |
| 863 | int str62net(const char *str, struct in6_addr *addr, unsigned char *mask) |
| 864 | { |
| 865 | char *c, *s; |
| 866 | int ret_val = 0; |
| 867 | char *err; |
| 868 | unsigned long len = 128; |
| 869 | |
| 870 | s = strdup(str); |
| 871 | if (!s) |
| 872 | return 0; |
| 873 | |
| 874 | memset(mask, 0, sizeof(*mask)); |
| 875 | memset(addr, 0, sizeof(*addr)); |
| 876 | |
| 877 | if ((c = strrchr(s, '/')) != NULL) { |
| 878 | *c++ = '\0'; /* c points to the mask */ |
| 879 | if (!*c) |
| 880 | goto out_free; |
| 881 | |
| 882 | len = strtoul(c, &err, 10); |
| 883 | if ((err && *err) || (unsigned)len > 128) |
| 884 | goto out_free; |
| 885 | } |
| 886 | *mask = len; /* OK we have a valid mask in <len> */ |
| 887 | |
| 888 | if (!inet_pton(AF_INET6, s, addr)) |
| 889 | goto out_free; |
| 890 | |
| 891 | ret_val = 1; |
| 892 | out_free: |
| 893 | free(s); |
| 894 | return ret_val; |
| 895 | } |
| 896 | |
| 897 | |
| 898 | /* |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 899 | * Parse IPv4 address found in url. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 900 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 901 | int url2ipv4(const char *addr, struct in_addr *dst) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 902 | { |
| 903 | int saw_digit, octets, ch; |
| 904 | u_char tmp[4], *tp; |
| 905 | const char *cp = addr; |
| 906 | |
| 907 | saw_digit = 0; |
| 908 | octets = 0; |
| 909 | *(tp = tmp) = 0; |
| 910 | |
| 911 | while (*addr) { |
| 912 | unsigned char digit = (ch = *addr++) - '0'; |
| 913 | if (digit > 9 && ch != '.') |
| 914 | break; |
| 915 | if (digit <= 9) { |
| 916 | u_int new = *tp * 10 + digit; |
| 917 | if (new > 255) |
| 918 | return 0; |
| 919 | *tp = new; |
| 920 | if (!saw_digit) { |
| 921 | if (++octets > 4) |
| 922 | return 0; |
| 923 | saw_digit = 1; |
| 924 | } |
| 925 | } else if (ch == '.' && saw_digit) { |
| 926 | if (octets == 4) |
| 927 | return 0; |
| 928 | *++tp = 0; |
| 929 | saw_digit = 0; |
| 930 | } else |
| 931 | return 0; |
| 932 | } |
| 933 | |
| 934 | if (octets < 4) |
| 935 | return 0; |
| 936 | |
| 937 | memcpy(&dst->s_addr, tmp, 4); |
| 938 | return addr-cp-1; |
| 939 | } |
| 940 | |
| 941 | /* |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 942 | * Resolve destination server from URL. Convert <str> to a sockaddr_storage. |
| 943 | * <out> contain the code of the dectected scheme, the start and length of |
| 944 | * the hostname. Actually only http and https are supported. <out> can be NULL. |
| 945 | * This function returns the consumed length. It is useful if you parse complete |
| 946 | * url like http://host:port/path, because the consumed length corresponds to |
| 947 | * the first character of the path. If the conversion fails, it returns -1. |
| 948 | * |
| 949 | * This function tries to resolve the DNS name if haproxy is in starting mode. |
| 950 | * So, this function may be used during the configuration parsing. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 951 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 952 | 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] | 953 | { |
| 954 | const char *curr = url, *cp = url; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 955 | const char *end; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 956 | int ret, url_code = 0; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 957 | unsigned long long int http_code = 0; |
| 958 | int default_port; |
| 959 | struct hostent *he; |
| 960 | char *p; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 961 | |
| 962 | /* Firstly, try to find :// pattern */ |
| 963 | while (curr < url+ulen && url_code != 0x3a2f2f) { |
| 964 | url_code = ((url_code & 0xffff) << 8); |
| 965 | url_code += (unsigned char)*curr++; |
| 966 | } |
| 967 | |
| 968 | /* Secondly, if :// pattern is found, verify parsed stuff |
| 969 | * before pattern is matching our http pattern. |
| 970 | * If so parse ip address and port in uri. |
| 971 | * |
| 972 | * WARNING: Current code doesn't support dynamic async dns resolver. |
| 973 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 974 | if (url_code != 0x3a2f2f) |
| 975 | return -1; |
| 976 | |
| 977 | /* Copy scheme, and utrn to lower case. */ |
| 978 | while (cp < curr - 3) |
| 979 | http_code = (http_code << 8) + *cp++; |
| 980 | http_code |= 0x2020202020202020ULL; /* Turn everything to lower case */ |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 981 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 982 | /* HTTP or HTTPS url matching */ |
| 983 | if (http_code == 0x2020202068747470ULL) { |
| 984 | default_port = 80; |
| 985 | if (out) |
| 986 | out->scheme = SCH_HTTP; |
| 987 | } |
| 988 | else if (http_code == 0x2020206874747073ULL) { |
| 989 | default_port = 443; |
| 990 | if (out) |
| 991 | out->scheme = SCH_HTTPS; |
| 992 | } |
| 993 | else |
| 994 | return -1; |
| 995 | |
| 996 | /* If the next char is '[', the host address is IPv6. */ |
| 997 | if (*curr == '[') { |
| 998 | curr++; |
| 999 | |
| 1000 | /* Check trash size */ |
| 1001 | if (trash.size < ulen) |
| 1002 | return -1; |
| 1003 | |
| 1004 | /* Look for ']' and copy the address in a trash buffer. */ |
| 1005 | p = trash.str; |
| 1006 | for (end = curr; |
| 1007 | end < url + ulen && *end != ']'; |
| 1008 | end++, p++) |
| 1009 | *p = *end; |
| 1010 | if (*end != ']') |
| 1011 | return -1; |
| 1012 | *p = '\0'; |
| 1013 | |
| 1014 | /* Update out. */ |
| 1015 | if (out) { |
| 1016 | out->host = curr; |
| 1017 | out->host_len = end - curr; |
| 1018 | } |
| 1019 | |
| 1020 | /* Try IPv6 decoding. */ |
| 1021 | if (!inet_pton(AF_INET6, trash.str, &((struct sockaddr_in6 *)addr)->sin6_addr)) |
| 1022 | return -1; |
| 1023 | end++; |
| 1024 | |
| 1025 | /* Decode port. */ |
| 1026 | if (*end == ':') { |
| 1027 | end++; |
| 1028 | default_port = read_uint(&end, url + ulen); |
| 1029 | } |
| 1030 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port); |
| 1031 | ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; |
| 1032 | return end - url; |
| 1033 | } |
| 1034 | else { |
| 1035 | /* We are looking for IP address. If you want to parse and |
| 1036 | * resolve hostname found in url, you can use str2sa_range(), but |
| 1037 | * be warned this can slow down global daemon performances |
| 1038 | * while handling lagging dns responses. |
| 1039 | */ |
| 1040 | ret = url2ipv4(curr, &((struct sockaddr_in *)addr)->sin_addr); |
| 1041 | if (ret) { |
| 1042 | /* Update out. */ |
| 1043 | if (out) { |
| 1044 | out->host = curr; |
| 1045 | out->host_len = ret; |
| 1046 | } |
| 1047 | |
| 1048 | curr += ret; |
| 1049 | |
| 1050 | /* Decode port. */ |
| 1051 | if (*curr == ':') { |
| 1052 | curr++; |
| 1053 | default_port = read_uint(&curr, url + ulen); |
| 1054 | } |
| 1055 | ((struct sockaddr_in *)addr)->sin_port = htons(default_port); |
| 1056 | |
| 1057 | /* Set family. */ |
| 1058 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 1059 | return curr - url; |
| 1060 | } |
| 1061 | else if (global.mode & MODE_STARTING) { |
| 1062 | /* The IPv4 and IPv6 decoding fails, maybe the url contain name. Try to execute |
| 1063 | * synchronous DNS request only if HAProxy is in the start state. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1064 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1065 | |
| 1066 | /* look for : or / or end */ |
| 1067 | for (end = curr; |
| 1068 | end < url + ulen && *end != '/' && *end != ':'; |
| 1069 | end++); |
| 1070 | memcpy(trash.str, curr, end - curr); |
| 1071 | trash.str[end - curr] = '\0'; |
| 1072 | |
| 1073 | /* try to resolve an IPv4/IPv6 hostname */ |
| 1074 | he = gethostbyname(trash.str); |
| 1075 | if (!he) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1076 | return -1; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1077 | |
| 1078 | /* Update out. */ |
| 1079 | if (out) { |
| 1080 | out->host = curr; |
| 1081 | out->host_len = end - curr; |
| 1082 | } |
| 1083 | |
| 1084 | /* Decode port. */ |
| 1085 | if (*end == ':') { |
| 1086 | end++; |
| 1087 | default_port = read_uint(&end, url + ulen); |
| 1088 | } |
| 1089 | |
| 1090 | /* Copy IP address, set port and family. */ |
| 1091 | switch (he->h_addrtype) { |
| 1092 | case AF_INET: |
| 1093 | ((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 1094 | ((struct sockaddr_in *)addr)->sin_port = htons(default_port); |
| 1095 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 1096 | return end - url; |
| 1097 | |
| 1098 | case AF_INET6: |
| 1099 | ((struct sockaddr_in6 *)addr)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list); |
| 1100 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port); |
| 1101 | ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; |
| 1102 | return end - url; |
| 1103 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1104 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1105 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1106 | return -1; |
| 1107 | } |
| 1108 | |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1109 | /* Tries to convert a sockaddr_storage address to text form. Upon success, the |
| 1110 | * address family is returned so that it's easy for the caller to adapt to the |
| 1111 | * output format. Zero is returned if the address family is not supported. -1 |
| 1112 | * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are |
| 1113 | * supported. |
| 1114 | */ |
| 1115 | int addr_to_str(struct sockaddr_storage *addr, char *str, int size) |
| 1116 | { |
| 1117 | |
| 1118 | void *ptr; |
| 1119 | |
| 1120 | if (size < 5) |
| 1121 | return 0; |
| 1122 | *str = '\0'; |
| 1123 | |
| 1124 | switch (addr->ss_family) { |
| 1125 | case AF_INET: |
| 1126 | ptr = &((struct sockaddr_in *)addr)->sin_addr; |
| 1127 | break; |
| 1128 | case AF_INET6: |
| 1129 | ptr = &((struct sockaddr_in6 *)addr)->sin6_addr; |
| 1130 | break; |
| 1131 | case AF_UNIX: |
| 1132 | memcpy(str, "unix", 5); |
| 1133 | return addr->ss_family; |
| 1134 | default: |
| 1135 | return 0; |
| 1136 | } |
| 1137 | |
| 1138 | if (inet_ntop(addr->ss_family, ptr, str, size)) |
| 1139 | return addr->ss_family; |
| 1140 | |
| 1141 | /* failed */ |
| 1142 | return -1; |
| 1143 | } |
| 1144 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1145 | /* will try to encode the string <string> replacing all characters tagged in |
| 1146 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 1147 | * prefixed by <escape>, and will store the result between <start> (included) |
| 1148 | * and <stop> (excluded), and will always terminate the string with a '\0' |
| 1149 | * before <stop>. The position of the '\0' is returned if the conversion |
| 1150 | * completes. If bytes are missing between <start> and <stop>, then the |
| 1151 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 1152 | * cannot even be stored so we return <start> without writing the 0. |
| 1153 | * The input string must also be zero-terminated. |
| 1154 | */ |
| 1155 | const char hextab[16] = "0123456789ABCDEF"; |
| 1156 | char *encode_string(char *start, char *stop, |
| 1157 | const char escape, const fd_set *map, |
| 1158 | const char *string) |
| 1159 | { |
| 1160 | if (start < stop) { |
| 1161 | stop--; /* reserve one byte for the final '\0' */ |
| 1162 | while (start < stop && *string != '\0') { |
| 1163 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 1164 | *start++ = *string; |
| 1165 | else { |
| 1166 | if (start + 3 >= stop) |
| 1167 | break; |
| 1168 | *start++ = escape; |
| 1169 | *start++ = hextab[(*string >> 4) & 15]; |
| 1170 | *start++ = hextab[*string & 15]; |
| 1171 | } |
| 1172 | string++; |
| 1173 | } |
| 1174 | *start = '\0'; |
| 1175 | } |
| 1176 | return start; |
| 1177 | } |
| 1178 | |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1179 | /* |
| 1180 | * Same behavior as encode_string() above, except that it encodes chunk |
| 1181 | * <chunk> instead of a string. |
| 1182 | */ |
| 1183 | char *encode_chunk(char *start, char *stop, |
| 1184 | const char escape, const fd_set *map, |
| 1185 | const struct chunk *chunk) |
| 1186 | { |
| 1187 | char *str = chunk->str; |
| 1188 | char *end = chunk->str + chunk->len; |
| 1189 | |
| 1190 | if (start < stop) { |
| 1191 | stop--; /* reserve one byte for the final '\0' */ |
| 1192 | while (start < stop && str < end) { |
| 1193 | if (!FD_ISSET((unsigned char)(*str), map)) |
| 1194 | *start++ = *str; |
| 1195 | else { |
| 1196 | if (start + 3 >= stop) |
| 1197 | break; |
| 1198 | *start++ = escape; |
| 1199 | *start++ = hextab[(*str >> 4) & 15]; |
| 1200 | *start++ = hextab[*str & 15]; |
| 1201 | } |
| 1202 | str++; |
| 1203 | } |
| 1204 | *start = '\0'; |
| 1205 | } |
| 1206 | return start; |
| 1207 | } |
| 1208 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1209 | /* Decode an URL-encoded string in-place. The resulting string might |
| 1210 | * be shorter. If some forbidden characters are found, the conversion is |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 1211 | * aborted, the string is truncated before the issue and a negative value is |
| 1212 | * returned, otherwise the operation returns the length of the decoded string. |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1213 | */ |
| 1214 | int url_decode(char *string) |
| 1215 | { |
| 1216 | char *in, *out; |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 1217 | int ret = -1; |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1218 | |
| 1219 | in = string; |
| 1220 | out = string; |
| 1221 | while (*in) { |
| 1222 | switch (*in) { |
| 1223 | case '+' : |
| 1224 | *out++ = ' '; |
| 1225 | break; |
| 1226 | case '%' : |
| 1227 | if (!ishex(in[1]) || !ishex(in[2])) |
| 1228 | goto end; |
| 1229 | *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]); |
| 1230 | in += 2; |
| 1231 | break; |
| 1232 | default: |
| 1233 | *out++ = *in; |
| 1234 | break; |
| 1235 | } |
| 1236 | in++; |
| 1237 | } |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 1238 | ret = out - string; /* success */ |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1239 | end: |
| 1240 | *out = 0; |
| 1241 | return ret; |
| 1242 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1243 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1244 | unsigned int str2ui(const char *s) |
| 1245 | { |
| 1246 | return __str2ui(s); |
| 1247 | } |
| 1248 | |
| 1249 | unsigned int str2uic(const char *s) |
| 1250 | { |
| 1251 | return __str2uic(s); |
| 1252 | } |
| 1253 | |
| 1254 | unsigned int strl2ui(const char *s, int len) |
| 1255 | { |
| 1256 | return __strl2ui(s, len); |
| 1257 | } |
| 1258 | |
| 1259 | unsigned int strl2uic(const char *s, int len) |
| 1260 | { |
| 1261 | return __strl2uic(s, len); |
| 1262 | } |
| 1263 | |
Willy Tarreau | 4ec83cd | 2010-10-15 23:19:55 +0200 | [diff] [blame] | 1264 | unsigned int read_uint(const char **s, const char *end) |
| 1265 | { |
| 1266 | return __read_uint(s, end); |
| 1267 | } |
| 1268 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1269 | /* This one is 7 times faster than strtol() on athlon with checks. |
| 1270 | * It returns the value of the number composed of all valid digits read, |
| 1271 | * and can process negative numbers too. |
| 1272 | */ |
| 1273 | int strl2ic(const char *s, int len) |
| 1274 | { |
| 1275 | int i = 0; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 1276 | int j, k; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1277 | |
| 1278 | if (len > 0) { |
| 1279 | if (*s != '-') { |
| 1280 | /* positive number */ |
| 1281 | while (len-- > 0) { |
| 1282 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 1283 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1284 | if (j > 9) |
| 1285 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 1286 | i = k + j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1287 | } |
| 1288 | } else { |
| 1289 | /* negative number */ |
| 1290 | s++; |
| 1291 | while (--len > 0) { |
| 1292 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 1293 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1294 | if (j > 9) |
| 1295 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 1296 | i = k - j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | } |
| 1300 | return i; |
| 1301 | } |
| 1302 | |
| 1303 | |
| 1304 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 1305 | * signed integer which it stores into <ret>. It accurately detects any error |
| 1306 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 1307 | * applications designed for hostile environments. It returns zero when the |
| 1308 | * number has successfully been converted, non-zero otherwise. When an error |
| 1309 | * is returned, the <ret> value is left untouched. It is yet 5 to 40 times |
| 1310 | * faster than strtol(). |
| 1311 | */ |
| 1312 | int strl2irc(const char *s, int len, int *ret) |
| 1313 | { |
| 1314 | int i = 0; |
| 1315 | int j; |
| 1316 | |
| 1317 | if (!len) |
| 1318 | return 1; |
| 1319 | |
| 1320 | if (*s != '-') { |
| 1321 | /* positive number */ |
| 1322 | while (len-- > 0) { |
| 1323 | j = (*s++) - '0'; |
| 1324 | if (j > 9) return 1; /* invalid char */ |
| 1325 | if (i > INT_MAX / 10) return 1; /* check for multiply overflow */ |
| 1326 | i = i * 10; |
| 1327 | if (i + j < i) return 1; /* check for addition overflow */ |
| 1328 | i = i + j; |
| 1329 | } |
| 1330 | } else { |
| 1331 | /* negative number */ |
| 1332 | s++; |
| 1333 | while (--len > 0) { |
| 1334 | j = (*s++) - '0'; |
| 1335 | if (j > 9) return 1; /* invalid char */ |
| 1336 | if (i < INT_MIN / 10) return 1; /* check for multiply overflow */ |
| 1337 | i = i * 10; |
| 1338 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 1339 | i = i - j; |
| 1340 | } |
| 1341 | } |
| 1342 | *ret = i; |
| 1343 | return 0; |
| 1344 | } |
| 1345 | |
| 1346 | |
| 1347 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 1348 | * signed integer which it stores into <ret>. It accurately detects any error |
| 1349 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 1350 | * applications designed for hostile environments. It returns zero when the |
| 1351 | * number has successfully been converted, non-zero otherwise. When an error |
| 1352 | * is returned, the <ret> value is left untouched. It is about 3 times slower |
| 1353 | * than str2irc(). |
| 1354 | */ |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1355 | |
| 1356 | int strl2llrc(const char *s, int len, long long *ret) |
| 1357 | { |
| 1358 | long long i = 0; |
| 1359 | int j; |
| 1360 | |
| 1361 | if (!len) |
| 1362 | return 1; |
| 1363 | |
| 1364 | if (*s != '-') { |
| 1365 | /* positive number */ |
| 1366 | while (len-- > 0) { |
| 1367 | j = (*s++) - '0'; |
| 1368 | if (j > 9) return 1; /* invalid char */ |
| 1369 | if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */ |
| 1370 | i = i * 10LL; |
| 1371 | if (i + j < i) return 1; /* check for addition overflow */ |
| 1372 | i = i + j; |
| 1373 | } |
| 1374 | } else { |
| 1375 | /* negative number */ |
| 1376 | s++; |
| 1377 | while (--len > 0) { |
| 1378 | j = (*s++) - '0'; |
| 1379 | if (j > 9) return 1; /* invalid char */ |
| 1380 | if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */ |
| 1381 | i = i * 10LL; |
| 1382 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 1383 | i = i - j; |
| 1384 | } |
| 1385 | } |
| 1386 | *ret = i; |
| 1387 | return 0; |
| 1388 | } |
| 1389 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 1390 | /* This function is used with pat_parse_dotted_ver(). It converts a string |
| 1391 | * composed by two number separated by a dot. Each part must contain in 16 bits |
| 1392 | * because internally they will be represented as a 32-bit quantity stored in |
| 1393 | * a 64-bit integer. It returns zero when the number has successfully been |
| 1394 | * converted, non-zero otherwise. When an error is returned, the <ret> value |
| 1395 | * is left untouched. |
| 1396 | * |
| 1397 | * "1.3" -> 0x0000000000010003 |
| 1398 | * "65535.65535" -> 0x00000000ffffffff |
| 1399 | */ |
| 1400 | int strl2llrc_dotted(const char *text, int len, long long *ret) |
| 1401 | { |
| 1402 | const char *end = &text[len]; |
| 1403 | const char *p; |
| 1404 | long long major, minor; |
| 1405 | |
| 1406 | /* Look for dot. */ |
| 1407 | for (p = text; p < end; p++) |
| 1408 | if (*p == '.') |
| 1409 | break; |
| 1410 | |
| 1411 | /* Convert major. */ |
| 1412 | if (strl2llrc(text, p - text, &major) != 0) |
| 1413 | return 1; |
| 1414 | |
| 1415 | /* Check major. */ |
| 1416 | if (major >= 65536) |
| 1417 | return 1; |
| 1418 | |
| 1419 | /* Convert minor. */ |
| 1420 | minor = 0; |
| 1421 | if (p < end) |
| 1422 | if (strl2llrc(p + 1, end - (p + 1), &minor) != 0) |
| 1423 | return 1; |
| 1424 | |
| 1425 | /* Check minor. */ |
| 1426 | if (minor >= 65536) |
| 1427 | return 1; |
| 1428 | |
| 1429 | /* Compose value. */ |
| 1430 | *ret = (major << 16) | (minor & 0xffff); |
| 1431 | return 0; |
| 1432 | } |
| 1433 | |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 1434 | /* This function parses a time value optionally followed by a unit suffix among |
| 1435 | * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit |
| 1436 | * expected by the caller. The computation does its best to avoid overflows. |
| 1437 | * The value is returned in <ret> if everything is fine, and a NULL is returned |
| 1438 | * by the function. In case of error, a pointer to the error is returned and |
| 1439 | * <ret> is left untouched. Values are automatically rounded up when needed. |
| 1440 | */ |
| 1441 | const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags) |
| 1442 | { |
| 1443 | unsigned imult, idiv; |
| 1444 | unsigned omult, odiv; |
| 1445 | unsigned value; |
| 1446 | |
| 1447 | omult = odiv = 1; |
| 1448 | |
| 1449 | switch (unit_flags & TIME_UNIT_MASK) { |
| 1450 | case TIME_UNIT_US: omult = 1000000; break; |
| 1451 | case TIME_UNIT_MS: omult = 1000; break; |
| 1452 | case TIME_UNIT_S: break; |
| 1453 | case TIME_UNIT_MIN: odiv = 60; break; |
| 1454 | case TIME_UNIT_HOUR: odiv = 3600; break; |
| 1455 | case TIME_UNIT_DAY: odiv = 86400; break; |
| 1456 | default: break; |
| 1457 | } |
| 1458 | |
| 1459 | value = 0; |
| 1460 | |
| 1461 | while (1) { |
| 1462 | unsigned int j; |
| 1463 | |
| 1464 | j = *text - '0'; |
| 1465 | if (j > 9) |
| 1466 | break; |
| 1467 | text++; |
| 1468 | value *= 10; |
| 1469 | value += j; |
| 1470 | } |
| 1471 | |
| 1472 | imult = idiv = 1; |
| 1473 | switch (*text) { |
| 1474 | case '\0': /* no unit = default unit */ |
| 1475 | imult = omult = idiv = odiv = 1; |
| 1476 | break; |
| 1477 | case 's': /* second = unscaled unit */ |
| 1478 | break; |
| 1479 | case 'u': /* microsecond : "us" */ |
| 1480 | if (text[1] == 's') { |
| 1481 | idiv = 1000000; |
| 1482 | text++; |
| 1483 | } |
| 1484 | break; |
| 1485 | case 'm': /* millisecond : "ms" or minute: "m" */ |
| 1486 | if (text[1] == 's') { |
| 1487 | idiv = 1000; |
| 1488 | text++; |
| 1489 | } else |
| 1490 | imult = 60; |
| 1491 | break; |
| 1492 | case 'h': /* hour : "h" */ |
| 1493 | imult = 3600; |
| 1494 | break; |
| 1495 | case 'd': /* day : "d" */ |
| 1496 | imult = 86400; |
| 1497 | break; |
| 1498 | default: |
| 1499 | return text; |
| 1500 | break; |
| 1501 | } |
| 1502 | |
| 1503 | if (omult % idiv == 0) { omult /= idiv; idiv = 1; } |
| 1504 | if (idiv % omult == 0) { idiv /= omult; omult = 1; } |
| 1505 | if (imult % odiv == 0) { imult /= odiv; odiv = 1; } |
| 1506 | if (odiv % imult == 0) { odiv /= imult; imult = 1; } |
| 1507 | |
| 1508 | value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv); |
| 1509 | *ret = value; |
| 1510 | return NULL; |
| 1511 | } |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1512 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 1513 | /* this function converts the string starting at <text> to an unsigned int |
| 1514 | * stored in <ret>. If an error is detected, the pointer to the unexpected |
| 1515 | * character is returned. If the conversio is succesful, NULL is returned. |
| 1516 | */ |
| 1517 | const char *parse_size_err(const char *text, unsigned *ret) { |
| 1518 | unsigned value = 0; |
| 1519 | |
| 1520 | while (1) { |
| 1521 | unsigned int j; |
| 1522 | |
| 1523 | j = *text - '0'; |
| 1524 | if (j > 9) |
| 1525 | break; |
| 1526 | if (value > ~0U / 10) |
| 1527 | return text; |
| 1528 | value *= 10; |
| 1529 | if (value > (value + j)) |
| 1530 | return text; |
| 1531 | value += j; |
| 1532 | text++; |
| 1533 | } |
| 1534 | |
| 1535 | switch (*text) { |
| 1536 | case '\0': |
| 1537 | break; |
| 1538 | case 'K': |
| 1539 | case 'k': |
| 1540 | if (value > ~0U >> 10) |
| 1541 | return text; |
| 1542 | value = value << 10; |
| 1543 | break; |
| 1544 | case 'M': |
| 1545 | case 'm': |
| 1546 | if (value > ~0U >> 20) |
| 1547 | return text; |
| 1548 | value = value << 20; |
| 1549 | break; |
| 1550 | case 'G': |
| 1551 | case 'g': |
| 1552 | if (value > ~0U >> 30) |
| 1553 | return text; |
| 1554 | value = value << 30; |
| 1555 | break; |
| 1556 | default: |
| 1557 | return text; |
| 1558 | } |
| 1559 | |
| 1560 | *ret = value; |
| 1561 | return NULL; |
| 1562 | } |
| 1563 | |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1564 | /* |
| 1565 | * Parse binary string written in hexadecimal (source) and store the decoded |
| 1566 | * result into binstr and set binstrlen to the lengh of binstr. Memory for |
| 1567 | * 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] | 1568 | * error message in err. In succes case, it returns the consumed length. |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1569 | */ |
| 1570 | int parse_binary(const char *source, char **binstr, int *binstrlen, char **err) |
| 1571 | { |
| 1572 | int len; |
| 1573 | const char *p = source; |
| 1574 | int i,j; |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 1575 | int alloc; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1576 | |
| 1577 | len = strlen(source); |
| 1578 | if (len % 2) { |
| 1579 | memprintf(err, "an even number of hex digit is expected"); |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | len = len >> 1; |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 1584 | |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1585 | if (!*binstr) { |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 1586 | *binstr = calloc(len, sizeof(char)); |
| 1587 | if (!*binstr) { |
| 1588 | memprintf(err, "out of memory while loading string pattern"); |
| 1589 | return 0; |
| 1590 | } |
| 1591 | alloc = 1; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1592 | } |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 1593 | else { |
| 1594 | if (*binstrlen < len) { |
| 1595 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 1596 | len, *binstrlen); |
| 1597 | return 0; |
| 1598 | } |
| 1599 | alloc = 0; |
| 1600 | } |
| 1601 | *binstrlen = len; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1602 | |
| 1603 | i = j = 0; |
| 1604 | while (j < len) { |
| 1605 | if (!ishex(p[i++])) |
| 1606 | goto bad_input; |
| 1607 | if (!ishex(p[i++])) |
| 1608 | goto bad_input; |
| 1609 | (*binstr)[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]); |
| 1610 | } |
Thierry FOURNIER | ee330af | 2014-01-21 11:36:14 +0100 | [diff] [blame] | 1611 | return len << 1; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1612 | |
| 1613 | bad_input: |
| 1614 | memprintf(err, "an hex digit is expected (found '%c')", p[i-1]); |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 1615 | if (alloc) |
| 1616 | free(binstr); |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1617 | return 0; |
| 1618 | } |
| 1619 | |
Willy Tarreau | 946ba59 | 2009-05-10 15:41:18 +0200 | [diff] [blame] | 1620 | /* copies at most <n> characters from <src> and always terminates with '\0' */ |
| 1621 | char *my_strndup(const char *src, int n) |
| 1622 | { |
| 1623 | int len = 0; |
| 1624 | char *ret; |
| 1625 | |
| 1626 | while (len < n && src[len]) |
| 1627 | len++; |
| 1628 | |
| 1629 | ret = (char *)malloc(len + 1); |
| 1630 | if (!ret) |
| 1631 | return ret; |
| 1632 | memcpy(ret, src, len); |
| 1633 | ret[len] = '\0'; |
| 1634 | return ret; |
| 1635 | } |
| 1636 | |
Baptiste Assmann | bb77c8e | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1637 | /* |
| 1638 | * search needle in haystack |
| 1639 | * returns the pointer if found, returns NULL otherwise |
| 1640 | */ |
| 1641 | const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) |
| 1642 | { |
| 1643 | const void *c = NULL; |
| 1644 | unsigned char f; |
| 1645 | |
| 1646 | if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen)) |
| 1647 | return NULL; |
| 1648 | |
| 1649 | f = *(char *)needle; |
| 1650 | c = haystack; |
| 1651 | while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) { |
| 1652 | if ((haystacklen - (c - haystack)) < needlelen) |
| 1653 | return NULL; |
| 1654 | |
| 1655 | if (memcmp(c, needle, needlelen) == 0) |
| 1656 | return c; |
| 1657 | ++c; |
| 1658 | } |
| 1659 | return NULL; |
| 1660 | } |
| 1661 | |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 1662 | /* This function returns the first unused key greater than or equal to <key> in |
| 1663 | * ID tree <root>. Zero is returned if no place is found. |
| 1664 | */ |
| 1665 | unsigned int get_next_id(struct eb_root *root, unsigned int key) |
| 1666 | { |
| 1667 | struct eb32_node *used; |
| 1668 | |
| 1669 | do { |
| 1670 | used = eb32_lookup_ge(root, key); |
| 1671 | if (!used || used->key > key) |
| 1672 | return key; /* key is available */ |
| 1673 | key++; |
| 1674 | } while (key); |
| 1675 | return key; |
| 1676 | } |
| 1677 | |
Willy Tarreau | 348238b | 2010-01-18 15:05:57 +0100 | [diff] [blame] | 1678 | /* This function compares a sample word possibly followed by blanks to another |
| 1679 | * clean word. The compare is case-insensitive. 1 is returned if both are equal, |
| 1680 | * otherwise zero. This intends to be used when checking HTTP headers for some |
| 1681 | * values. Note that it validates a word followed only by blanks but does not |
| 1682 | * validate a word followed by blanks then other chars. |
| 1683 | */ |
| 1684 | int word_match(const char *sample, int slen, const char *word, int wlen) |
| 1685 | { |
| 1686 | if (slen < wlen) |
| 1687 | return 0; |
| 1688 | |
| 1689 | while (wlen) { |
| 1690 | char c = *sample ^ *word; |
| 1691 | if (c && c != ('A' ^ 'a')) |
| 1692 | return 0; |
| 1693 | sample++; |
| 1694 | word++; |
| 1695 | slen--; |
| 1696 | wlen--; |
| 1697 | } |
| 1698 | |
| 1699 | while (slen) { |
| 1700 | if (*sample != ' ' && *sample != '\t') |
| 1701 | return 0; |
| 1702 | sample++; |
| 1703 | slen--; |
| 1704 | } |
| 1705 | return 1; |
| 1706 | } |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 1707 | |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 1708 | /* Converts any text-formatted IPv4 address to a host-order IPv4 address. It |
| 1709 | * is particularly fast because it avoids expensive operations such as |
| 1710 | * multiplies, which are optimized away at the end. It requires a properly |
| 1711 | * formated address though (3 points). |
| 1712 | */ |
| 1713 | unsigned int inetaddr_host(const char *text) |
| 1714 | { |
| 1715 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 1716 | register unsigned int dig100, dig10, dig1; |
| 1717 | int s; |
| 1718 | const char *p, *d; |
| 1719 | |
| 1720 | dig1 = dig10 = dig100 = ascii_zero; |
| 1721 | s = 24; |
| 1722 | |
| 1723 | p = text; |
| 1724 | while (1) { |
| 1725 | if (((unsigned)(*p - '0')) <= 9) { |
| 1726 | p++; |
| 1727 | continue; |
| 1728 | } |
| 1729 | |
| 1730 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1731 | if (p == text) |
| 1732 | goto end; |
| 1733 | |
| 1734 | d = p - 1; |
| 1735 | dig1 |= (unsigned int)(*d << s); |
| 1736 | if (d == text) |
| 1737 | goto end; |
| 1738 | |
| 1739 | d--; |
| 1740 | dig10 |= (unsigned int)(*d << s); |
| 1741 | if (d == text) |
| 1742 | goto end; |
| 1743 | |
| 1744 | d--; |
| 1745 | dig100 |= (unsigned int)(*d << s); |
| 1746 | end: |
| 1747 | if (!s || *p != '.') |
| 1748 | break; |
| 1749 | |
| 1750 | s -= 8; |
| 1751 | text = ++p; |
| 1752 | } |
| 1753 | |
| 1754 | dig100 -= ascii_zero; |
| 1755 | dig10 -= ascii_zero; |
| 1756 | dig1 -= ascii_zero; |
| 1757 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1758 | } |
| 1759 | |
| 1760 | /* |
| 1761 | * Idem except the first unparsed character has to be passed in <stop>. |
| 1762 | */ |
| 1763 | unsigned int inetaddr_host_lim(const char *text, const char *stop) |
| 1764 | { |
| 1765 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 1766 | register unsigned int dig100, dig10, dig1; |
| 1767 | int s; |
| 1768 | const char *p, *d; |
| 1769 | |
| 1770 | dig1 = dig10 = dig100 = ascii_zero; |
| 1771 | s = 24; |
| 1772 | |
| 1773 | p = text; |
| 1774 | while (1) { |
| 1775 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 1776 | p++; |
| 1777 | continue; |
| 1778 | } |
| 1779 | |
| 1780 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1781 | if (p == text) |
| 1782 | goto end; |
| 1783 | |
| 1784 | d = p - 1; |
| 1785 | dig1 |= (unsigned int)(*d << s); |
| 1786 | if (d == text) |
| 1787 | goto end; |
| 1788 | |
| 1789 | d--; |
| 1790 | dig10 |= (unsigned int)(*d << s); |
| 1791 | if (d == text) |
| 1792 | goto end; |
| 1793 | |
| 1794 | d--; |
| 1795 | dig100 |= (unsigned int)(*d << s); |
| 1796 | end: |
| 1797 | if (!s || p == stop || *p != '.') |
| 1798 | break; |
| 1799 | |
| 1800 | s -= 8; |
| 1801 | text = ++p; |
| 1802 | } |
| 1803 | |
| 1804 | dig100 -= ascii_zero; |
| 1805 | dig10 -= ascii_zero; |
| 1806 | dig1 -= ascii_zero; |
| 1807 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1808 | } |
| 1809 | |
| 1810 | /* |
| 1811 | * Idem except the pointer to first unparsed byte is returned into <ret> which |
| 1812 | * must not be NULL. |
| 1813 | */ |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 1814 | unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret) |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 1815 | { |
| 1816 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 1817 | register unsigned int dig100, dig10, dig1; |
| 1818 | int s; |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 1819 | char *p, *d; |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 1820 | |
| 1821 | dig1 = dig10 = dig100 = ascii_zero; |
| 1822 | s = 24; |
| 1823 | |
| 1824 | p = text; |
| 1825 | while (1) { |
| 1826 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 1827 | p++; |
| 1828 | continue; |
| 1829 | } |
| 1830 | |
| 1831 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1832 | if (p == text) |
| 1833 | goto end; |
| 1834 | |
| 1835 | d = p - 1; |
| 1836 | dig1 |= (unsigned int)(*d << s); |
| 1837 | if (d == text) |
| 1838 | goto end; |
| 1839 | |
| 1840 | d--; |
| 1841 | dig10 |= (unsigned int)(*d << s); |
| 1842 | if (d == text) |
| 1843 | goto end; |
| 1844 | |
| 1845 | d--; |
| 1846 | dig100 |= (unsigned int)(*d << s); |
| 1847 | end: |
| 1848 | if (!s || p == stop || *p != '.') |
| 1849 | break; |
| 1850 | |
| 1851 | s -= 8; |
| 1852 | text = ++p; |
| 1853 | } |
| 1854 | |
| 1855 | *ret = p; |
| 1856 | dig100 -= ascii_zero; |
| 1857 | dig10 -= ascii_zero; |
| 1858 | dig1 -= ascii_zero; |
| 1859 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1860 | } |
| 1861 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 1862 | /* Convert a fixed-length string to an IP address. Returns 0 in case of error, |
| 1863 | * or the number of chars read in case of success. Maybe this could be replaced |
| 1864 | * by one of the functions above. Also, apparently this function does not support |
| 1865 | * hosts above 255 and requires exactly 4 octets. |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 1866 | * The destination is only modified on success. |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 1867 | */ |
| 1868 | int buf2ip(const char *buf, size_t len, struct in_addr *dst) |
| 1869 | { |
| 1870 | const char *addr; |
| 1871 | int saw_digit, octets, ch; |
| 1872 | u_char tmp[4], *tp; |
| 1873 | const char *cp = buf; |
| 1874 | |
| 1875 | saw_digit = 0; |
| 1876 | octets = 0; |
| 1877 | *(tp = tmp) = 0; |
| 1878 | |
| 1879 | for (addr = buf; addr - buf < len; addr++) { |
| 1880 | unsigned char digit = (ch = *addr) - '0'; |
| 1881 | |
| 1882 | if (digit > 9 && ch != '.') |
| 1883 | break; |
| 1884 | |
| 1885 | if (digit <= 9) { |
| 1886 | u_int new = *tp * 10 + digit; |
| 1887 | |
| 1888 | if (new > 255) |
| 1889 | return 0; |
| 1890 | |
| 1891 | *tp = new; |
| 1892 | |
| 1893 | if (!saw_digit) { |
| 1894 | if (++octets > 4) |
| 1895 | return 0; |
| 1896 | saw_digit = 1; |
| 1897 | } |
| 1898 | } else if (ch == '.' && saw_digit) { |
| 1899 | if (octets == 4) |
| 1900 | return 0; |
| 1901 | |
| 1902 | *++tp = 0; |
| 1903 | saw_digit = 0; |
| 1904 | } else |
| 1905 | return 0; |
| 1906 | } |
| 1907 | |
| 1908 | if (octets < 4) |
| 1909 | return 0; |
| 1910 | |
| 1911 | memcpy(&dst->s_addr, tmp, 4); |
| 1912 | return addr - cp; |
| 1913 | } |
| 1914 | |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1915 | /* This function converts the string in <buf> of the len <len> to |
| 1916 | * struct in6_addr <dst> which must be allocated by the caller. |
| 1917 | * This function returns 1 in success case, otherwise zero. |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 1918 | * The destination is only modified on success. |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1919 | */ |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1920 | int buf2ip6(const char *buf, size_t len, struct in6_addr *dst) |
| 1921 | { |
Thierry FOURNIER | cd65991 | 2013-12-11 12:33:54 +0100 | [diff] [blame] | 1922 | char null_term_ip6[INET6_ADDRSTRLEN + 1]; |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 1923 | struct in6_addr out; |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1924 | |
Thierry FOURNIER | cd65991 | 2013-12-11 12:33:54 +0100 | [diff] [blame] | 1925 | if (len > INET6_ADDRSTRLEN) |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1926 | return 0; |
| 1927 | |
| 1928 | memcpy(null_term_ip6, buf, len); |
| 1929 | null_term_ip6[len] = '\0'; |
| 1930 | |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 1931 | if (!inet_pton(AF_INET6, null_term_ip6, &out)) |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1932 | return 0; |
| 1933 | |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 1934 | *dst = out; |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1935 | return 1; |
| 1936 | } |
| 1937 | |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 1938 | /* To be used to quote config arg positions. Returns the short string at <ptr> |
| 1939 | * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line" |
| 1940 | * if ptr is NULL or empty. The string is locally allocated. |
| 1941 | */ |
| 1942 | const char *quote_arg(const char *ptr) |
| 1943 | { |
| 1944 | static char val[32]; |
| 1945 | int i; |
| 1946 | |
| 1947 | if (!ptr || !*ptr) |
| 1948 | return "end of line"; |
| 1949 | val[0] = '\''; |
Willy Tarreau | de2dd6b | 2013-01-24 02:14:42 +0100 | [diff] [blame] | 1950 | for (i = 1; i < sizeof(val) - 2 && *ptr; i++) |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 1951 | val[i] = *ptr++; |
| 1952 | val[i++] = '\''; |
| 1953 | val[i] = '\0'; |
| 1954 | return val; |
| 1955 | } |
| 1956 | |
Willy Tarreau | 5b18020 | 2010-07-18 10:40:48 +0200 | [diff] [blame] | 1957 | /* returns an operator among STD_OP_* for string <str> or < 0 if unknown */ |
| 1958 | int get_std_op(const char *str) |
| 1959 | { |
| 1960 | int ret = -1; |
| 1961 | |
| 1962 | if (*str == 'e' && str[1] == 'q') |
| 1963 | ret = STD_OP_EQ; |
| 1964 | else if (*str == 'n' && str[1] == 'e') |
| 1965 | ret = STD_OP_NE; |
| 1966 | else if (*str == 'l') { |
| 1967 | if (str[1] == 'e') ret = STD_OP_LE; |
| 1968 | else if (str[1] == 't') ret = STD_OP_LT; |
| 1969 | } |
| 1970 | else if (*str == 'g') { |
| 1971 | if (str[1] == 'e') ret = STD_OP_GE; |
| 1972 | else if (str[1] == 't') ret = STD_OP_GT; |
| 1973 | } |
| 1974 | |
| 1975 | if (ret == -1 || str[2] != '\0') |
| 1976 | return -1; |
| 1977 | return ret; |
| 1978 | } |
| 1979 | |
Willy Tarreau | 4c14eaa | 2010-11-24 14:01:45 +0100 | [diff] [blame] | 1980 | /* hash a 32-bit integer to another 32-bit integer */ |
| 1981 | unsigned int full_hash(unsigned int a) |
| 1982 | { |
| 1983 | return __full_hash(a); |
| 1984 | } |
| 1985 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 1986 | /* Return non-zero if IPv4 address is part of the network, |
| 1987 | * otherwise zero. |
| 1988 | */ |
| 1989 | int in_net_ipv4(struct in_addr *addr, struct in_addr *mask, struct in_addr *net) |
| 1990 | { |
| 1991 | return((addr->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr)); |
| 1992 | } |
| 1993 | |
| 1994 | /* Return non-zero if IPv6 address is part of the network, |
| 1995 | * otherwise zero. |
| 1996 | */ |
| 1997 | int in_net_ipv6(struct in6_addr *addr, struct in6_addr *mask, struct in6_addr *net) |
| 1998 | { |
| 1999 | int i; |
| 2000 | |
| 2001 | for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++) |
| 2002 | if (((((int *)addr)[i] & ((int *)mask)[i])) != |
| 2003 | (((int *)net)[i] & ((int *)mask)[i])) |
| 2004 | return 0; |
| 2005 | return 1; |
| 2006 | } |
| 2007 | |
| 2008 | /* RFC 4291 prefix */ |
| 2009 | const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00, |
| 2010 | 0x00, 0x00, 0x00, 0x00, |
| 2011 | 0x00, 0x00, 0xFF, 0xFF }; |
| 2012 | |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 2013 | /* Map IPv4 adress on IPv6 address, as specified in RFC 3513. |
| 2014 | * Input and output may overlap. |
| 2015 | */ |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 2016 | void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr) |
| 2017 | { |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 2018 | struct in_addr tmp_addr; |
| 2019 | |
| 2020 | tmp_addr.s_addr = sin_addr->s_addr; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 2021 | memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)); |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 2022 | memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | /* Map IPv6 adress on IPv4 address, as specified in RFC 3513. |
| 2026 | * Return true if conversion is possible and false otherwise. |
| 2027 | */ |
| 2028 | int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr) |
| 2029 | { |
| 2030 | if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) { |
| 2031 | memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]), |
| 2032 | sizeof(struct in_addr)); |
| 2033 | return 1; |
| 2034 | } |
| 2035 | |
| 2036 | return 0; |
| 2037 | } |
| 2038 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2039 | char *human_time(int t, short hz_div) { |
| 2040 | static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s" |
| 2041 | char *p = rv; |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2042 | char *end = rv + sizeof(rv); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2043 | int cnt=2; // print two numbers |
| 2044 | |
| 2045 | if (unlikely(t < 0 || hz_div <= 0)) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2046 | snprintf(p, end - p, "?"); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2047 | return rv; |
| 2048 | } |
| 2049 | |
| 2050 | if (unlikely(hz_div > 1)) |
| 2051 | t /= hz_div; |
| 2052 | |
| 2053 | if (t >= DAY) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2054 | p += snprintf(p, end - p, "%dd", t / DAY); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2055 | cnt--; |
| 2056 | } |
| 2057 | |
| 2058 | if (cnt && t % DAY / HOUR) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2059 | p += snprintf(p, end - p, "%dh", t % DAY / HOUR); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2060 | cnt--; |
| 2061 | } |
| 2062 | |
| 2063 | if (cnt && t % HOUR / MINUTE) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2064 | p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2065 | cnt--; |
| 2066 | } |
| 2067 | |
| 2068 | if ((cnt && t % MINUTE) || !t) // also display '0s' |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2069 | p += snprintf(p, end - p, "%ds", t % MINUTE / SEC); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2070 | |
| 2071 | return rv; |
| 2072 | } |
| 2073 | |
| 2074 | const char *monthname[12] = { |
| 2075 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 2076 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 2077 | }; |
| 2078 | |
| 2079 | /* date2str_log: write a date in the format : |
| 2080 | * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d", |
| 2081 | * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 2082 | * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000); |
| 2083 | * |
| 2084 | * without using sprintf. return a pointer to the last char written (\0) or |
| 2085 | * NULL if there isn't enough space. |
| 2086 | */ |
| 2087 | char *date2str_log(char *dst, struct tm *tm, struct timeval *date, size_t size) |
| 2088 | { |
| 2089 | |
| 2090 | if (size < 25) /* the size is fixed: 24 chars + \0 */ |
| 2091 | return NULL; |
| 2092 | |
| 2093 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
| 2094 | *dst++ = '/'; |
| 2095 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 2096 | dst += 3; |
| 2097 | *dst++ = '/'; |
| 2098 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
| 2099 | *dst++ = ':'; |
| 2100 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
| 2101 | *dst++ = ':'; |
| 2102 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
| 2103 | *dst++ = ':'; |
| 2104 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
| 2105 | *dst++ = '.'; |
| 2106 | utoa_pad((unsigned int)(date->tv_usec/1000), dst, 4); // millisecondes |
| 2107 | dst += 3; // only the 3 first digits |
| 2108 | *dst = '\0'; |
| 2109 | |
| 2110 | return dst; |
| 2111 | } |
| 2112 | |
| 2113 | /* gmt2str_log: write a date in the format : |
| 2114 | * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf |
| 2115 | * return a pointer to the last char written (\0) or |
| 2116 | * NULL if there isn't enough space. |
| 2117 | */ |
| 2118 | char *gmt2str_log(char *dst, struct tm *tm, size_t size) |
| 2119 | { |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 2120 | if (size < 27) /* the size is fixed: 26 chars + \0 */ |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2121 | return NULL; |
| 2122 | |
| 2123 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
| 2124 | *dst++ = '/'; |
| 2125 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 2126 | dst += 3; |
| 2127 | *dst++ = '/'; |
| 2128 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
| 2129 | *dst++ = ':'; |
| 2130 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
| 2131 | *dst++ = ':'; |
| 2132 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
| 2133 | *dst++ = ':'; |
| 2134 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
| 2135 | *dst++ = ' '; |
| 2136 | *dst++ = '+'; |
| 2137 | *dst++ = '0'; |
| 2138 | *dst++ = '0'; |
| 2139 | *dst++ = '0'; |
| 2140 | *dst++ = '0'; |
| 2141 | *dst = '\0'; |
| 2142 | |
| 2143 | return dst; |
| 2144 | } |
| 2145 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 2146 | /* localdate2str_log: write a date in the format : |
| 2147 | * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf |
| 2148 | * * return a pointer to the last char written (\0) or |
| 2149 | * * NULL if there isn't enough space. |
| 2150 | */ |
| 2151 | char *localdate2str_log(char *dst, struct tm *tm, size_t size) |
| 2152 | { |
| 2153 | if (size < 27) /* the size is fixed: 26 chars + \0 */ |
| 2154 | return NULL; |
| 2155 | |
| 2156 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
| 2157 | *dst++ = '/'; |
| 2158 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 2159 | dst += 3; |
| 2160 | *dst++ = '/'; |
| 2161 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
| 2162 | *dst++ = ':'; |
| 2163 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
| 2164 | *dst++ = ':'; |
| 2165 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
| 2166 | *dst++ = ':'; |
| 2167 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
| 2168 | *dst++ = ' '; |
| 2169 | memcpy(dst, localtimezone, 5); // timezone |
| 2170 | dst += 5; |
| 2171 | *dst = '\0'; |
| 2172 | |
| 2173 | return dst; |
| 2174 | } |
| 2175 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 2176 | /* Dynamically allocates a string of the proper length to hold the formatted |
| 2177 | * output. NULL is returned on error. The caller is responsible for freeing the |
| 2178 | * memory area using free(). The resulting string is returned in <out> if the |
| 2179 | * pointer is not NULL. A previous version of <out> might be used to build the |
| 2180 | * new string, and it will be freed before returning if it is not NULL, which |
| 2181 | * makes it possible to build complex strings from iterative calls without |
| 2182 | * having to care about freeing intermediate values, as in the example below : |
| 2183 | * |
| 2184 | * memprintf(&err, "invalid argument: '%s'", arg); |
| 2185 | * ... |
| 2186 | * memprintf(&err, "parser said : <%s>\n", *err); |
| 2187 | * ... |
| 2188 | * free(*err); |
| 2189 | * |
| 2190 | * This means that <err> must be initialized to NULL before first invocation. |
| 2191 | * The return value also holds the allocated string, which eases error checking |
| 2192 | * 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] | 2193 | * passed instead and it will be ignored. The returned message will then also |
| 2194 | * 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] | 2195 | * |
| 2196 | * It is also convenient to use it without any free except the last one : |
| 2197 | * err = NULL; |
| 2198 | * if (!fct1(err)) report(*err); |
| 2199 | * if (!fct2(err)) report(*err); |
| 2200 | * if (!fct3(err)) report(*err); |
| 2201 | * free(*err); |
| 2202 | */ |
| 2203 | char *memprintf(char **out, const char *format, ...) |
| 2204 | { |
| 2205 | va_list args; |
| 2206 | char *ret = NULL; |
| 2207 | int allocated = 0; |
| 2208 | int needed = 0; |
| 2209 | |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2210 | if (!out) |
| 2211 | return NULL; |
| 2212 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 2213 | do { |
| 2214 | /* vsnprintf() will return the required length even when the |
| 2215 | * target buffer is NULL. We do this in a loop just in case |
| 2216 | * intermediate evaluations get wrong. |
| 2217 | */ |
| 2218 | va_start(args, format); |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 2219 | needed = vsnprintf(ret, allocated, format, args); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 2220 | va_end(args); |
| 2221 | |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 2222 | if (needed < allocated) { |
| 2223 | /* Note: on Solaris 8, the first iteration always |
| 2224 | * returns -1 if allocated is zero, so we force a |
| 2225 | * retry. |
| 2226 | */ |
| 2227 | if (!allocated) |
| 2228 | needed = 0; |
| 2229 | else |
| 2230 | break; |
| 2231 | } |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 2232 | |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 2233 | allocated = needed + 1; |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 2234 | ret = realloc(ret, allocated); |
| 2235 | } while (ret); |
| 2236 | |
| 2237 | if (needed < 0) { |
| 2238 | /* an error was encountered */ |
| 2239 | free(ret); |
| 2240 | ret = NULL; |
| 2241 | } |
| 2242 | |
| 2243 | if (out) { |
| 2244 | free(*out); |
| 2245 | *out = ret; |
| 2246 | } |
| 2247 | |
| 2248 | return ret; |
| 2249 | } |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2250 | |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 2251 | /* Used to add <level> spaces before each line of <out>, unless there is only one line. |
| 2252 | * 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] | 2253 | * freed by the caller. It also supports being passed a NULL which results in the same |
| 2254 | * output. |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 2255 | * Example of use : |
| 2256 | * parse(cmd, &err); (callee: memprintf(&err, ...)) |
| 2257 | * fprintf(stderr, "Parser said: %s\n", indent_error(&err)); |
| 2258 | * free(err); |
| 2259 | */ |
| 2260 | char *indent_msg(char **out, int level) |
| 2261 | { |
| 2262 | char *ret, *in, *p; |
| 2263 | int needed = 0; |
| 2264 | int lf = 0; |
| 2265 | int lastlf = 0; |
| 2266 | int len; |
| 2267 | |
Willy Tarreau | 70eec38 | 2012-10-10 08:56:47 +0200 | [diff] [blame] | 2268 | if (!out || !*out) |
| 2269 | return NULL; |
| 2270 | |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 2271 | in = *out - 1; |
| 2272 | while ((in = strchr(in + 1, '\n')) != NULL) { |
| 2273 | lastlf = in - *out; |
| 2274 | lf++; |
| 2275 | } |
| 2276 | |
| 2277 | if (!lf) /* single line, no LF, return it as-is */ |
| 2278 | return *out; |
| 2279 | |
| 2280 | len = strlen(*out); |
| 2281 | |
| 2282 | if (lf == 1 && lastlf == len - 1) { |
| 2283 | /* single line, LF at end, strip it and return as-is */ |
| 2284 | (*out)[lastlf] = 0; |
| 2285 | return *out; |
| 2286 | } |
| 2287 | |
| 2288 | /* OK now we have at least one LF, we need to process the whole string |
| 2289 | * as a multi-line string. What we'll do : |
| 2290 | * - prefix with an LF if there is none |
| 2291 | * - add <level> spaces before each line |
| 2292 | * This means at most ( 1 + level + (len-lf) + lf*<1+level) ) = |
| 2293 | * 1 + level + len + lf * level = 1 + level * (lf + 1) + len. |
| 2294 | */ |
| 2295 | |
| 2296 | needed = 1 + level * (lf + 1) + len + 1; |
| 2297 | p = ret = malloc(needed); |
| 2298 | in = *out; |
| 2299 | |
| 2300 | /* skip initial LFs */ |
| 2301 | while (*in == '\n') |
| 2302 | in++; |
| 2303 | |
| 2304 | /* copy each line, prefixed with LF and <level> spaces, and without the trailing LF */ |
| 2305 | while (*in) { |
| 2306 | *p++ = '\n'; |
| 2307 | memset(p, ' ', level); |
| 2308 | p += level; |
| 2309 | do { |
| 2310 | *p++ = *in++; |
| 2311 | } while (*in && *in != '\n'); |
| 2312 | if (*in) |
| 2313 | in++; |
| 2314 | } |
| 2315 | *p = 0; |
| 2316 | |
| 2317 | free(*out); |
| 2318 | *out = ret; |
| 2319 | |
| 2320 | return ret; |
| 2321 | } |
| 2322 | |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 2323 | /* Convert occurrences of environment variables in the input string to their |
| 2324 | * corresponding value. A variable is identified as a series of alphanumeric |
| 2325 | * characters or underscores following a '$' sign. The <in> string must be |
| 2326 | * free()able. NULL returns NULL. The resulting string might be reallocated if |
| 2327 | * some expansion is made. Variable names may also be enclosed into braces if |
| 2328 | * needed (eg: to concatenate alphanum characters). |
| 2329 | */ |
| 2330 | char *env_expand(char *in) |
| 2331 | { |
| 2332 | char *txt_beg; |
| 2333 | char *out; |
| 2334 | char *txt_end; |
| 2335 | char *var_beg; |
| 2336 | char *var_end; |
| 2337 | char *value; |
| 2338 | char *next; |
| 2339 | int out_len; |
| 2340 | int val_len; |
| 2341 | |
| 2342 | if (!in) |
| 2343 | return in; |
| 2344 | |
| 2345 | value = out = NULL; |
| 2346 | out_len = 0; |
| 2347 | |
| 2348 | txt_beg = in; |
| 2349 | do { |
| 2350 | /* look for next '$' sign in <in> */ |
| 2351 | for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++); |
| 2352 | |
| 2353 | if (!*txt_end && !out) /* end and no expansion performed */ |
| 2354 | return in; |
| 2355 | |
| 2356 | val_len = 0; |
| 2357 | next = txt_end; |
| 2358 | if (*txt_end == '$') { |
| 2359 | char save; |
| 2360 | |
| 2361 | var_beg = txt_end + 1; |
| 2362 | if (*var_beg == '{') |
| 2363 | var_beg++; |
| 2364 | |
| 2365 | var_end = var_beg; |
| 2366 | while (isalnum((int)(unsigned char)*var_end) || *var_end == '_') { |
| 2367 | var_end++; |
| 2368 | } |
| 2369 | |
| 2370 | next = var_end; |
| 2371 | if (*var_end == '}' && (var_beg > txt_end + 1)) |
| 2372 | next++; |
| 2373 | |
| 2374 | /* get value of the variable name at this location */ |
| 2375 | save = *var_end; |
| 2376 | *var_end = '\0'; |
| 2377 | value = getenv(var_beg); |
| 2378 | *var_end = save; |
| 2379 | val_len = value ? strlen(value) : 0; |
| 2380 | } |
| 2381 | |
| 2382 | out = realloc(out, out_len + (txt_end - txt_beg) + val_len + 1); |
| 2383 | if (txt_end > txt_beg) { |
| 2384 | memcpy(out + out_len, txt_beg, txt_end - txt_beg); |
| 2385 | out_len += txt_end - txt_beg; |
| 2386 | } |
| 2387 | if (val_len) { |
| 2388 | memcpy(out + out_len, value, val_len); |
| 2389 | out_len += val_len; |
| 2390 | } |
| 2391 | out[out_len] = 0; |
| 2392 | txt_beg = next; |
| 2393 | } while (*txt_beg); |
| 2394 | |
| 2395 | /* here we know that <out> was allocated and that we don't need <in> anymore */ |
| 2396 | free(in); |
| 2397 | return out; |
| 2398 | } |
| 2399 | |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 2400 | |
| 2401 | /* same as strstr() but case-insensitive and with limit length */ |
| 2402 | const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2) |
| 2403 | { |
| 2404 | char *pptr, *sptr, *start; |
| 2405 | uint slen, plen; |
| 2406 | uint tmp1, tmp2; |
| 2407 | |
| 2408 | if (str1 == NULL || len_str1 == 0) // search pattern into an empty string => search is not found |
| 2409 | return NULL; |
| 2410 | |
| 2411 | if (str2 == NULL || len_str2 == 0) // pattern is empty => every str1 match |
| 2412 | return str1; |
| 2413 | |
| 2414 | if (len_str1 < len_str2) // pattern is longer than string => search is not found |
| 2415 | return NULL; |
| 2416 | |
| 2417 | for (tmp1 = 0, start = (char *)str1, pptr = (char *)str2, slen = len_str1, plen = len_str2; slen >= plen; start++, slen--) { |
| 2418 | while (toupper(*start) != toupper(*str2)) { |
| 2419 | start++; |
| 2420 | slen--; |
| 2421 | tmp1++; |
| 2422 | |
| 2423 | if (tmp1 >= len_str1) |
| 2424 | return NULL; |
| 2425 | |
| 2426 | /* if pattern longer than string */ |
| 2427 | if (slen < plen) |
| 2428 | return NULL; |
| 2429 | } |
| 2430 | |
| 2431 | sptr = start; |
| 2432 | pptr = (char *)str2; |
| 2433 | |
| 2434 | tmp2 = 0; |
| 2435 | while (toupper(*sptr) == toupper(*pptr)) { |
| 2436 | sptr++; |
| 2437 | pptr++; |
| 2438 | tmp2++; |
| 2439 | |
| 2440 | if (*pptr == '\0' || tmp2 == len_str2) /* end of pattern found */ |
| 2441 | return start; |
| 2442 | if (*sptr == '\0' || tmp2 == len_str1) /* end of string found and the pattern is not fully found */ |
| 2443 | return NULL; |
| 2444 | } |
| 2445 | } |
| 2446 | return NULL; |
| 2447 | } |
| 2448 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2449 | /* |
| 2450 | * Local variables: |
| 2451 | * c-indent-level: 8 |
| 2452 | * c-basic-offset: 8 |
| 2453 | * End: |
| 2454 | */ |