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 | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 638 | * - 'abns@' -> force address to belong to the abstract namespace (Linux |
| 639 | * only). These sockets are just like Unix sockets but without |
| 640 | * the need for an underlying file system. The address is a |
| 641 | * string. Technically it's like a Unix socket with a zero in |
| 642 | * the first byte of the address. |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 643 | * - "fd@" => an integer must follow, and is a file descriptor number. |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 644 | * |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 645 | * Also note that in order to avoid any ambiguity with IPv6 addresses, the ':' |
| 646 | * is mandatory after the IP address even when no port is specified. NULL is |
| 647 | * 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] | 648 | * always initialized if non-null, even for non-IP families. |
Willy Tarreau | d393a62 | 2013-03-04 18:22:00 +0100 | [diff] [blame] | 649 | * |
| 650 | * If <pfx> is non-null, it is used as a string prefix before any path-based |
| 651 | * address (typically the path to a unix socket). |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 652 | * |
| 653 | * When a file descriptor is passed, its value is put into the s_addr part of |
| 654 | * 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] | 655 | */ |
Willy Tarreau | d393a62 | 2013-03-04 18:22:00 +0100 | [diff] [blame] | 656 | 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] | 657 | { |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 658 | static struct sockaddr_storage ss; |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 659 | struct sockaddr_storage *ret = NULL; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 660 | char *back, *str2; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 661 | char *port1, *port2; |
| 662 | int portl, porth, porta; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 663 | int abstract = 0; |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 664 | |
| 665 | portl = porth = porta = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 666 | |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 667 | str2 = back = env_expand(strdup(str)); |
Willy Tarreau | df350f1 | 2013-03-01 20:22:54 +0100 | [diff] [blame] | 668 | if (str2 == NULL) { |
| 669 | memprintf(err, "out of memory in '%s'\n", __FUNCTION__); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 670 | goto out; |
Willy Tarreau | df350f1 | 2013-03-01 20:22:54 +0100 | [diff] [blame] | 671 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 672 | |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 673 | memset(&ss, 0, sizeof(ss)); |
| 674 | |
| 675 | if (strncmp(str2, "unix@", 5) == 0) { |
| 676 | str2 += 5; |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 677 | abstract = 0; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 678 | ss.ss_family = AF_UNIX; |
| 679 | } |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 680 | else if (strncmp(str2, "abns@", 5) == 0) { |
| 681 | str2 += 5; |
| 682 | abstract = 1; |
| 683 | ss.ss_family = AF_UNIX; |
| 684 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 685 | else if (strncmp(str2, "ipv4@", 5) == 0) { |
| 686 | str2 += 5; |
| 687 | ss.ss_family = AF_INET; |
| 688 | } |
| 689 | else if (strncmp(str2, "ipv6@", 5) == 0) { |
| 690 | str2 += 5; |
| 691 | ss.ss_family = AF_INET6; |
| 692 | } |
| 693 | else if (*str2 == '/') { |
| 694 | ss.ss_family = AF_UNIX; |
| 695 | } |
| 696 | else |
| 697 | ss.ss_family = AF_UNSPEC; |
| 698 | |
Willy Tarreau | 40aa070 | 2013-03-10 23:51:38 +0100 | [diff] [blame] | 699 | if (ss.ss_family == AF_UNSPEC && strncmp(str2, "fd@", 3) == 0) { |
| 700 | char *endptr; |
| 701 | |
| 702 | str2 += 3; |
| 703 | ((struct sockaddr_in *)&ss)->sin_addr.s_addr = strtol(str2, &endptr, 10); |
| 704 | |
| 705 | if (!*str2 || *endptr) { |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 706 | 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] | 707 | goto out; |
| 708 | } |
| 709 | |
| 710 | /* we return AF_UNSPEC if we use a file descriptor number */ |
| 711 | ss.ss_family = AF_UNSPEC; |
| 712 | } |
| 713 | else if (ss.ss_family == AF_UNIX) { |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 714 | int prefix_path_len; |
| 715 | int max_path_len; |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 716 | int adr_len; |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 717 | |
| 718 | /* complete unix socket path name during startup or soft-restart is |
| 719 | * <unix_bind_prefix><path>.<pid>.<bak|tmp> |
| 720 | */ |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 721 | prefix_path_len = (pfx && !abstract) ? strlen(pfx) : 0; |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 722 | max_path_len = (sizeof(((struct sockaddr_un *)&ss)->sun_path) - 1) - |
| 723 | (prefix_path_len ? prefix_path_len + 1 + 5 + 1 + 3 : 0); |
| 724 | |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 725 | adr_len = strlen(str2); |
| 726 | if (adr_len > max_path_len) { |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 727 | memprintf(err, "socket path '%s' too long (max %d)\n", str, max_path_len); |
| 728 | goto out; |
| 729 | } |
| 730 | |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 731 | /* when abstract==1, we skip the first zero and copy all bytes except the trailing zero */ |
| 732 | memset(((struct sockaddr_un *)&ss)->sun_path, 0, sizeof(((struct sockaddr_un *)&ss)->sun_path)); |
Willy Tarreau | 94ef3f3 | 2014-04-14 14:49:00 +0200 | [diff] [blame] | 733 | if (prefix_path_len) |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 734 | memcpy(((struct sockaddr_un *)&ss)->sun_path, pfx, prefix_path_len); |
Willy Tarreau | ccfccef | 2014-05-10 01:49:15 +0200 | [diff] [blame] | 735 | memcpy(((struct sockaddr_un *)&ss)->sun_path + prefix_path_len + abstract, str2, adr_len + 1 - abstract); |
Willy Tarreau | 1558638 | 2013-03-04 19:48:14 +0100 | [diff] [blame] | 736 | } |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 737 | else { /* IPv4 and IPv6 */ |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 738 | port1 = strrchr(str2, ':'); |
| 739 | if (port1) |
| 740 | *port1++ = '\0'; |
| 741 | else |
| 742 | port1 = ""; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 743 | |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 744 | if (str2ip(str2, &ss) == NULL) { |
| 745 | memprintf(err, "invalid address: '%s' in '%s'\n", str2, str); |
| 746 | goto out; |
| 747 | } |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 748 | |
Willy Tarreau | a39d199 | 2013-04-01 20:37:42 +0200 | [diff] [blame] | 749 | if (isdigit((int)(unsigned char)*port1)) { /* single port or range */ |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 750 | port2 = strchr(port1, '-'); |
| 751 | if (port2) |
| 752 | *port2++ = '\0'; |
| 753 | else |
| 754 | port2 = port1; |
| 755 | portl = atoi(port1); |
| 756 | porth = atoi(port2); |
| 757 | porta = portl; |
| 758 | } |
| 759 | else if (*port1 == '-') { /* negative offset */ |
| 760 | portl = atoi(port1 + 1); |
| 761 | porta = -portl; |
| 762 | } |
| 763 | else if (*port1 == '+') { /* positive offset */ |
| 764 | porth = atoi(port1 + 1); |
| 765 | porta = porth; |
| 766 | } |
| 767 | else if (*port1) { /* other any unexpected char */ |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 768 | 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] | 769 | goto out; |
| 770 | } |
| 771 | set_host_port(&ss, porta); |
Willy Tarreau | e4c58c8 | 2013-03-06 15:28:17 +0100 | [diff] [blame] | 772 | } |
Willy Tarreau | fab5a43 | 2011-03-04 15:31:53 +0100 | [diff] [blame] | 773 | |
Willy Tarreau | c120c8d | 2013-03-10 19:27:44 +0100 | [diff] [blame] | 774 | ret = &ss; |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 775 | out: |
Willy Tarreau | d4448bc | 2013-02-20 15:55:15 +0100 | [diff] [blame] | 776 | if (low) |
| 777 | *low = portl; |
| 778 | if (high) |
| 779 | *high = porth; |
Willy Tarreau | 2470928 | 2013-03-10 21:32:12 +0100 | [diff] [blame] | 780 | free(back); |
Willy Tarreau | d5191e7 | 2010-02-09 20:50:45 +0100 | [diff] [blame] | 781 | return ret; |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 782 | } |
| 783 | |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 784 | /* converts <str> to a struct in_addr containing a network mask. It can be |
| 785 | * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1 |
| 786 | * if the conversion succeeds otherwise non-zero. |
| 787 | */ |
| 788 | int str2mask(const char *str, struct in_addr *mask) |
| 789 | { |
| 790 | if (strchr(str, '.') != NULL) { /* dotted notation */ |
| 791 | if (!inet_pton(AF_INET, str, mask)) |
| 792 | return 0; |
| 793 | } |
| 794 | else { /* mask length */ |
| 795 | char *err; |
| 796 | unsigned long len = strtol(str, &err, 10); |
| 797 | |
| 798 | if (!*str || (err && *err) || (unsigned)len > 32) |
| 799 | return 0; |
| 800 | if (len) |
| 801 | mask->s_addr = htonl(~0UL << (32 - len)); |
| 802 | else |
| 803 | mask->s_addr = 0; |
| 804 | } |
| 805 | return 1; |
| 806 | } |
| 807 | |
Thierry FOURNIER | b050463 | 2013-12-14 15:39:02 +0100 | [diff] [blame] | 808 | /* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion |
| 809 | * succeeds otherwise zero. |
| 810 | */ |
| 811 | int cidr2dotted(int cidr, struct in_addr *mask) { |
| 812 | |
| 813 | if (cidr < 0 || cidr > 32) |
| 814 | return 0; |
| 815 | |
| 816 | mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0; |
| 817 | return 1; |
| 818 | } |
| 819 | |
Willy Tarreau | c6f4ce8 | 2009-06-10 11:09:37 +0200 | [diff] [blame] | 820 | /* |
Willy Tarreau | d077a8e | 2007-05-08 18:28:09 +0200 | [diff] [blame] | 821 | * converts <str> to two struct in_addr* which must be pre-allocated. |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 822 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 823 | * is optionnal and either in the dotted or CIDR notation. |
| 824 | * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error. |
| 825 | */ |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 826 | 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] | 827 | { |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 828 | __label__ out_free, out_err; |
| 829 | char *c, *s; |
| 830 | int ret_val; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 831 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 832 | s = strdup(str); |
| 833 | if (!s) |
| 834 | return 0; |
| 835 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 836 | memset(mask, 0, sizeof(*mask)); |
| 837 | memset(addr, 0, sizeof(*addr)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 838 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 839 | if ((c = strrchr(s, '/')) != NULL) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 840 | *c++ = '\0'; |
| 841 | /* c points to the mask */ |
Willy Tarreau | 2937c0d | 2010-01-26 17:36:17 +0100 | [diff] [blame] | 842 | if (!str2mask(c, mask)) |
| 843 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 844 | } |
| 845 | else { |
Willy Tarreau | ebd6160 | 2006-12-30 11:54:15 +0100 | [diff] [blame] | 846 | mask->s_addr = ~0U; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 847 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 848 | if (!inet_pton(AF_INET, s, addr)) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 849 | struct hostent *he; |
| 850 | |
Thierry FOURNIER | fc7ac7b | 2014-02-11 15:23:04 +0100 | [diff] [blame] | 851 | if (!resolve) |
| 852 | goto out_err; |
| 853 | |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 854 | if ((he = gethostbyname(s)) == NULL) { |
| 855 | goto out_err; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 856 | } |
| 857 | else |
| 858 | *addr = *(struct in_addr *) *(he->h_addr_list); |
| 859 | } |
Willy Tarreau | 8aeae4a | 2007-06-17 11:42:08 +0200 | [diff] [blame] | 860 | |
| 861 | ret_val = 1; |
| 862 | out_free: |
| 863 | free(s); |
| 864 | return ret_val; |
| 865 | out_err: |
| 866 | ret_val = 0; |
| 867 | goto out_free; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 868 | } |
| 869 | |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 870 | |
| 871 | /* |
Willy Tarreau | 6d20e28 | 2012-04-27 22:49:47 +0200 | [diff] [blame] | 872 | * converts <str> to two struct in6_addr* which must be pre-allocated. |
| 873 | * The format is "addr[/mask]", where "addr" cannot be empty, and mask |
| 874 | * is an optionnal number of bits (128 being the default). |
| 875 | * Returns 1 if OK, 0 if error. |
| 876 | */ |
| 877 | int str62net(const char *str, struct in6_addr *addr, unsigned char *mask) |
| 878 | { |
| 879 | char *c, *s; |
| 880 | int ret_val = 0; |
| 881 | char *err; |
| 882 | unsigned long len = 128; |
| 883 | |
| 884 | s = strdup(str); |
| 885 | if (!s) |
| 886 | return 0; |
| 887 | |
| 888 | memset(mask, 0, sizeof(*mask)); |
| 889 | memset(addr, 0, sizeof(*addr)); |
| 890 | |
| 891 | if ((c = strrchr(s, '/')) != NULL) { |
| 892 | *c++ = '\0'; /* c points to the mask */ |
| 893 | if (!*c) |
| 894 | goto out_free; |
| 895 | |
| 896 | len = strtoul(c, &err, 10); |
| 897 | if ((err && *err) || (unsigned)len > 128) |
| 898 | goto out_free; |
| 899 | } |
| 900 | *mask = len; /* OK we have a valid mask in <len> */ |
| 901 | |
| 902 | if (!inet_pton(AF_INET6, s, addr)) |
| 903 | goto out_free; |
| 904 | |
| 905 | ret_val = 1; |
| 906 | out_free: |
| 907 | free(s); |
| 908 | return ret_val; |
| 909 | } |
| 910 | |
| 911 | |
| 912 | /* |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 913 | * Parse IPv4 address found in url. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 914 | */ |
David du Colombier | 6f5ccb1 | 2011-03-10 22:26:24 +0100 | [diff] [blame] | 915 | int url2ipv4(const char *addr, struct in_addr *dst) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 916 | { |
| 917 | int saw_digit, octets, ch; |
| 918 | u_char tmp[4], *tp; |
| 919 | const char *cp = addr; |
| 920 | |
| 921 | saw_digit = 0; |
| 922 | octets = 0; |
| 923 | *(tp = tmp) = 0; |
| 924 | |
| 925 | while (*addr) { |
| 926 | unsigned char digit = (ch = *addr++) - '0'; |
| 927 | if (digit > 9 && ch != '.') |
| 928 | break; |
| 929 | if (digit <= 9) { |
| 930 | u_int new = *tp * 10 + digit; |
| 931 | if (new > 255) |
| 932 | return 0; |
| 933 | *tp = new; |
| 934 | if (!saw_digit) { |
| 935 | if (++octets > 4) |
| 936 | return 0; |
| 937 | saw_digit = 1; |
| 938 | } |
| 939 | } else if (ch == '.' && saw_digit) { |
| 940 | if (octets == 4) |
| 941 | return 0; |
| 942 | *++tp = 0; |
| 943 | saw_digit = 0; |
| 944 | } else |
| 945 | return 0; |
| 946 | } |
| 947 | |
| 948 | if (octets < 4) |
| 949 | return 0; |
| 950 | |
| 951 | memcpy(&dst->s_addr, tmp, 4); |
| 952 | return addr-cp-1; |
| 953 | } |
| 954 | |
| 955 | /* |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 956 | * Resolve destination server from URL. Convert <str> to a sockaddr_storage. |
| 957 | * <out> contain the code of the dectected scheme, the start and length of |
| 958 | * the hostname. Actually only http and https are supported. <out> can be NULL. |
| 959 | * This function returns the consumed length. It is useful if you parse complete |
| 960 | * url like http://host:port/path, because the consumed length corresponds to |
| 961 | * the first character of the path. If the conversion fails, it returns -1. |
| 962 | * |
| 963 | * This function tries to resolve the DNS name if haproxy is in starting mode. |
| 964 | * So, this function may be used during the configuration parsing. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 965 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 966 | 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] | 967 | { |
| 968 | const char *curr = url, *cp = url; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 969 | const char *end; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 970 | int ret, url_code = 0; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 971 | unsigned long long int http_code = 0; |
| 972 | int default_port; |
| 973 | struct hostent *he; |
| 974 | char *p; |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 975 | |
| 976 | /* Firstly, try to find :// pattern */ |
| 977 | while (curr < url+ulen && url_code != 0x3a2f2f) { |
| 978 | url_code = ((url_code & 0xffff) << 8); |
| 979 | url_code += (unsigned char)*curr++; |
| 980 | } |
| 981 | |
| 982 | /* Secondly, if :// pattern is found, verify parsed stuff |
| 983 | * before pattern is matching our http pattern. |
| 984 | * If so parse ip address and port in uri. |
| 985 | * |
| 986 | * WARNING: Current code doesn't support dynamic async dns resolver. |
| 987 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 988 | if (url_code != 0x3a2f2f) |
| 989 | return -1; |
| 990 | |
| 991 | /* Copy scheme, and utrn to lower case. */ |
| 992 | while (cp < curr - 3) |
| 993 | http_code = (http_code << 8) + *cp++; |
| 994 | http_code |= 0x2020202020202020ULL; /* Turn everything to lower case */ |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 995 | |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 996 | /* HTTP or HTTPS url matching */ |
| 997 | if (http_code == 0x2020202068747470ULL) { |
| 998 | default_port = 80; |
| 999 | if (out) |
| 1000 | out->scheme = SCH_HTTP; |
| 1001 | } |
| 1002 | else if (http_code == 0x2020206874747073ULL) { |
| 1003 | default_port = 443; |
| 1004 | if (out) |
| 1005 | out->scheme = SCH_HTTPS; |
| 1006 | } |
| 1007 | else |
| 1008 | return -1; |
| 1009 | |
| 1010 | /* If the next char is '[', the host address is IPv6. */ |
| 1011 | if (*curr == '[') { |
| 1012 | curr++; |
| 1013 | |
| 1014 | /* Check trash size */ |
| 1015 | if (trash.size < ulen) |
| 1016 | return -1; |
| 1017 | |
| 1018 | /* Look for ']' and copy the address in a trash buffer. */ |
| 1019 | p = trash.str; |
| 1020 | for (end = curr; |
| 1021 | end < url + ulen && *end != ']'; |
| 1022 | end++, p++) |
| 1023 | *p = *end; |
| 1024 | if (*end != ']') |
| 1025 | return -1; |
| 1026 | *p = '\0'; |
| 1027 | |
| 1028 | /* Update out. */ |
| 1029 | if (out) { |
| 1030 | out->host = curr; |
| 1031 | out->host_len = end - curr; |
| 1032 | } |
| 1033 | |
| 1034 | /* Try IPv6 decoding. */ |
| 1035 | if (!inet_pton(AF_INET6, trash.str, &((struct sockaddr_in6 *)addr)->sin6_addr)) |
| 1036 | return -1; |
| 1037 | end++; |
| 1038 | |
| 1039 | /* Decode port. */ |
| 1040 | if (*end == ':') { |
| 1041 | end++; |
| 1042 | default_port = read_uint(&end, url + ulen); |
| 1043 | } |
| 1044 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port); |
| 1045 | ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; |
| 1046 | return end - url; |
| 1047 | } |
| 1048 | else { |
| 1049 | /* We are looking for IP address. If you want to parse and |
| 1050 | * resolve hostname found in url, you can use str2sa_range(), but |
| 1051 | * be warned this can slow down global daemon performances |
| 1052 | * while handling lagging dns responses. |
| 1053 | */ |
| 1054 | ret = url2ipv4(curr, &((struct sockaddr_in *)addr)->sin_addr); |
| 1055 | if (ret) { |
| 1056 | /* Update out. */ |
| 1057 | if (out) { |
| 1058 | out->host = curr; |
| 1059 | out->host_len = ret; |
| 1060 | } |
| 1061 | |
| 1062 | curr += ret; |
| 1063 | |
| 1064 | /* Decode port. */ |
| 1065 | if (*curr == ':') { |
| 1066 | curr++; |
| 1067 | default_port = read_uint(&curr, url + ulen); |
| 1068 | } |
| 1069 | ((struct sockaddr_in *)addr)->sin_port = htons(default_port); |
| 1070 | |
| 1071 | /* Set family. */ |
| 1072 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 1073 | return curr - url; |
| 1074 | } |
| 1075 | else if (global.mode & MODE_STARTING) { |
| 1076 | /* The IPv4 and IPv6 decoding fails, maybe the url contain name. Try to execute |
| 1077 | * synchronous DNS request only if HAProxy is in the start state. |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1078 | */ |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1079 | |
| 1080 | /* look for : or / or end */ |
| 1081 | for (end = curr; |
| 1082 | end < url + ulen && *end != '/' && *end != ':'; |
| 1083 | end++); |
| 1084 | memcpy(trash.str, curr, end - curr); |
| 1085 | trash.str[end - curr] = '\0'; |
| 1086 | |
| 1087 | /* try to resolve an IPv4/IPv6 hostname */ |
| 1088 | he = gethostbyname(trash.str); |
| 1089 | if (!he) |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1090 | return -1; |
Thierry FOURNIER | 9f95e40 | 2014-03-21 14:51:46 +0100 | [diff] [blame] | 1091 | |
| 1092 | /* Update out. */ |
| 1093 | if (out) { |
| 1094 | out->host = curr; |
| 1095 | out->host_len = end - curr; |
| 1096 | } |
| 1097 | |
| 1098 | /* Decode port. */ |
| 1099 | if (*end == ':') { |
| 1100 | end++; |
| 1101 | default_port = read_uint(&end, url + ulen); |
| 1102 | } |
| 1103 | |
| 1104 | /* Copy IP address, set port and family. */ |
| 1105 | switch (he->h_addrtype) { |
| 1106 | case AF_INET: |
| 1107 | ((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *) *(he->h_addr_list); |
| 1108 | ((struct sockaddr_in *)addr)->sin_port = htons(default_port); |
| 1109 | ((struct sockaddr_in *)addr)->sin_family = AF_INET; |
| 1110 | return end - url; |
| 1111 | |
| 1112 | case AF_INET6: |
| 1113 | ((struct sockaddr_in6 *)addr)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list); |
| 1114 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port); |
| 1115 | ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6; |
| 1116 | return end - url; |
| 1117 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1118 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1119 | } |
Alexandre Cassen | 5eb1a90 | 2007-11-29 15:43:32 +0100 | [diff] [blame] | 1120 | return -1; |
| 1121 | } |
| 1122 | |
Willy Tarreau | 631f01c | 2011-09-05 00:36:48 +0200 | [diff] [blame] | 1123 | /* Tries to convert a sockaddr_storage address to text form. Upon success, the |
| 1124 | * address family is returned so that it's easy for the caller to adapt to the |
| 1125 | * output format. Zero is returned if the address family is not supported. -1 |
| 1126 | * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are |
| 1127 | * supported. |
| 1128 | */ |
| 1129 | int addr_to_str(struct sockaddr_storage *addr, char *str, int size) |
| 1130 | { |
| 1131 | |
| 1132 | void *ptr; |
| 1133 | |
| 1134 | if (size < 5) |
| 1135 | return 0; |
| 1136 | *str = '\0'; |
| 1137 | |
| 1138 | switch (addr->ss_family) { |
| 1139 | case AF_INET: |
| 1140 | ptr = &((struct sockaddr_in *)addr)->sin_addr; |
| 1141 | break; |
| 1142 | case AF_INET6: |
| 1143 | ptr = &((struct sockaddr_in6 *)addr)->sin6_addr; |
| 1144 | break; |
| 1145 | case AF_UNIX: |
| 1146 | memcpy(str, "unix", 5); |
| 1147 | return addr->ss_family; |
| 1148 | default: |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
| 1152 | if (inet_ntop(addr->ss_family, ptr, str, size)) |
| 1153 | return addr->ss_family; |
| 1154 | |
| 1155 | /* failed */ |
| 1156 | return -1; |
| 1157 | } |
| 1158 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1159 | /* will try to encode the string <string> replacing all characters tagged in |
| 1160 | * <map> with the hexadecimal representation of their ASCII-code (2 digits) |
| 1161 | * prefixed by <escape>, and will store the result between <start> (included) |
| 1162 | * and <stop> (excluded), and will always terminate the string with a '\0' |
| 1163 | * before <stop>. The position of the '\0' is returned if the conversion |
| 1164 | * completes. If bytes are missing between <start> and <stop>, then the |
| 1165 | * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0' |
| 1166 | * cannot even be stored so we return <start> without writing the 0. |
| 1167 | * The input string must also be zero-terminated. |
| 1168 | */ |
| 1169 | const char hextab[16] = "0123456789ABCDEF"; |
| 1170 | char *encode_string(char *start, char *stop, |
| 1171 | const char escape, const fd_set *map, |
| 1172 | const char *string) |
| 1173 | { |
| 1174 | if (start < stop) { |
| 1175 | stop--; /* reserve one byte for the final '\0' */ |
| 1176 | while (start < stop && *string != '\0') { |
| 1177 | if (!FD_ISSET((unsigned char)(*string), map)) |
| 1178 | *start++ = *string; |
| 1179 | else { |
| 1180 | if (start + 3 >= stop) |
| 1181 | break; |
| 1182 | *start++ = escape; |
| 1183 | *start++ = hextab[(*string >> 4) & 15]; |
| 1184 | *start++ = hextab[*string & 15]; |
| 1185 | } |
| 1186 | string++; |
| 1187 | } |
| 1188 | *start = '\0'; |
| 1189 | } |
| 1190 | return start; |
| 1191 | } |
| 1192 | |
Thierry FOURNIER | e059ec9 | 2014-03-17 12:01:13 +0100 | [diff] [blame] | 1193 | /* |
| 1194 | * Same behavior as encode_string() above, except that it encodes chunk |
| 1195 | * <chunk> instead of a string. |
| 1196 | */ |
| 1197 | char *encode_chunk(char *start, char *stop, |
| 1198 | const char escape, const fd_set *map, |
| 1199 | const struct chunk *chunk) |
| 1200 | { |
| 1201 | char *str = chunk->str; |
| 1202 | char *end = chunk->str + chunk->len; |
| 1203 | |
| 1204 | if (start < stop) { |
| 1205 | stop--; /* reserve one byte for the final '\0' */ |
| 1206 | while (start < stop && str < end) { |
| 1207 | if (!FD_ISSET((unsigned char)(*str), map)) |
| 1208 | *start++ = *str; |
| 1209 | else { |
| 1210 | if (start + 3 >= stop) |
| 1211 | break; |
| 1212 | *start++ = escape; |
| 1213 | *start++ = hextab[(*str >> 4) & 15]; |
| 1214 | *start++ = hextab[*str & 15]; |
| 1215 | } |
| 1216 | str++; |
| 1217 | } |
| 1218 | *start = '\0'; |
| 1219 | } |
| 1220 | return start; |
| 1221 | } |
| 1222 | |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1223 | /* Decode an URL-encoded string in-place. The resulting string might |
| 1224 | * be shorter. If some forbidden characters are found, the conversion is |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 1225 | * aborted, the string is truncated before the issue and a negative value is |
| 1226 | * returned, otherwise the operation returns the length of the decoded string. |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1227 | */ |
| 1228 | int url_decode(char *string) |
| 1229 | { |
| 1230 | char *in, *out; |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 1231 | int ret = -1; |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1232 | |
| 1233 | in = string; |
| 1234 | out = string; |
| 1235 | while (*in) { |
| 1236 | switch (*in) { |
| 1237 | case '+' : |
| 1238 | *out++ = ' '; |
| 1239 | break; |
| 1240 | case '%' : |
| 1241 | if (!ishex(in[1]) || !ishex(in[2])) |
| 1242 | goto end; |
| 1243 | *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]); |
| 1244 | in += 2; |
| 1245 | break; |
| 1246 | default: |
| 1247 | *out++ = *in; |
| 1248 | break; |
| 1249 | } |
| 1250 | in++; |
| 1251 | } |
Thierry FOURNIER | 5068d96 | 2013-10-04 16:27:27 +0200 | [diff] [blame] | 1252 | ret = out - string; /* success */ |
Willy Tarreau | bf9c2fc | 2011-05-31 18:06:18 +0200 | [diff] [blame] | 1253 | end: |
| 1254 | *out = 0; |
| 1255 | return ret; |
| 1256 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1257 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1258 | unsigned int str2ui(const char *s) |
| 1259 | { |
| 1260 | return __str2ui(s); |
| 1261 | } |
| 1262 | |
| 1263 | unsigned int str2uic(const char *s) |
| 1264 | { |
| 1265 | return __str2uic(s); |
| 1266 | } |
| 1267 | |
| 1268 | unsigned int strl2ui(const char *s, int len) |
| 1269 | { |
| 1270 | return __strl2ui(s, len); |
| 1271 | } |
| 1272 | |
| 1273 | unsigned int strl2uic(const char *s, int len) |
| 1274 | { |
| 1275 | return __strl2uic(s, len); |
| 1276 | } |
| 1277 | |
Willy Tarreau | 4ec83cd | 2010-10-15 23:19:55 +0200 | [diff] [blame] | 1278 | unsigned int read_uint(const char **s, const char *end) |
| 1279 | { |
| 1280 | return __read_uint(s, end); |
| 1281 | } |
| 1282 | |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1283 | /* This one is 7 times faster than strtol() on athlon with checks. |
| 1284 | * It returns the value of the number composed of all valid digits read, |
| 1285 | * and can process negative numbers too. |
| 1286 | */ |
| 1287 | int strl2ic(const char *s, int len) |
| 1288 | { |
| 1289 | int i = 0; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 1290 | int j, k; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1291 | |
| 1292 | if (len > 0) { |
| 1293 | if (*s != '-') { |
| 1294 | /* positive number */ |
| 1295 | while (len-- > 0) { |
| 1296 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 1297 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1298 | if (j > 9) |
| 1299 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 1300 | i = k + j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1301 | } |
| 1302 | } else { |
| 1303 | /* negative number */ |
| 1304 | s++; |
| 1305 | while (--len > 0) { |
| 1306 | j = (*s++) - '0'; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 1307 | k = i * 10; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1308 | if (j > 9) |
| 1309 | break; |
Willy Tarreau | 3f0c976 | 2007-10-25 09:42:24 +0200 | [diff] [blame] | 1310 | i = k - j; |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1311 | } |
| 1312 | } |
| 1313 | } |
| 1314 | return i; |
| 1315 | } |
| 1316 | |
| 1317 | |
| 1318 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 1319 | * signed integer which it stores into <ret>. It accurately detects any error |
| 1320 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 1321 | * applications designed for hostile environments. It returns zero when the |
| 1322 | * number has successfully been converted, non-zero otherwise. When an error |
| 1323 | * is returned, the <ret> value is left untouched. It is yet 5 to 40 times |
| 1324 | * faster than strtol(). |
| 1325 | */ |
| 1326 | int strl2irc(const char *s, int len, int *ret) |
| 1327 | { |
| 1328 | int i = 0; |
| 1329 | int j; |
| 1330 | |
| 1331 | if (!len) |
| 1332 | return 1; |
| 1333 | |
| 1334 | if (*s != '-') { |
| 1335 | /* positive number */ |
| 1336 | while (len-- > 0) { |
| 1337 | j = (*s++) - '0'; |
| 1338 | if (j > 9) return 1; /* invalid char */ |
| 1339 | if (i > INT_MAX / 10) return 1; /* check for multiply overflow */ |
| 1340 | i = i * 10; |
| 1341 | if (i + j < i) return 1; /* check for addition overflow */ |
| 1342 | i = i + j; |
| 1343 | } |
| 1344 | } else { |
| 1345 | /* negative number */ |
| 1346 | s++; |
| 1347 | while (--len > 0) { |
| 1348 | j = (*s++) - '0'; |
| 1349 | if (j > 9) return 1; /* invalid char */ |
| 1350 | if (i < INT_MIN / 10) return 1; /* check for multiply overflow */ |
| 1351 | i = i * 10; |
| 1352 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 1353 | i = i - j; |
| 1354 | } |
| 1355 | } |
| 1356 | *ret = i; |
| 1357 | return 0; |
| 1358 | } |
| 1359 | |
| 1360 | |
| 1361 | /* This function reads exactly <len> chars from <s> and converts them to a |
| 1362 | * signed integer which it stores into <ret>. It accurately detects any error |
| 1363 | * (truncated string, invalid chars, overflows). It is meant to be used in |
| 1364 | * applications designed for hostile environments. It returns zero when the |
| 1365 | * number has successfully been converted, non-zero otherwise. When an error |
| 1366 | * is returned, the <ret> value is left untouched. It is about 3 times slower |
| 1367 | * than str2irc(). |
| 1368 | */ |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1369 | |
| 1370 | int strl2llrc(const char *s, int len, long long *ret) |
| 1371 | { |
| 1372 | long long i = 0; |
| 1373 | int j; |
| 1374 | |
| 1375 | if (!len) |
| 1376 | return 1; |
| 1377 | |
| 1378 | if (*s != '-') { |
| 1379 | /* positive number */ |
| 1380 | while (len-- > 0) { |
| 1381 | j = (*s++) - '0'; |
| 1382 | if (j > 9) return 1; /* invalid char */ |
| 1383 | if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */ |
| 1384 | i = i * 10LL; |
| 1385 | if (i + j < i) return 1; /* check for addition overflow */ |
| 1386 | i = i + j; |
| 1387 | } |
| 1388 | } else { |
| 1389 | /* negative number */ |
| 1390 | s++; |
| 1391 | while (--len > 0) { |
| 1392 | j = (*s++) - '0'; |
| 1393 | if (j > 9) return 1; /* invalid char */ |
| 1394 | if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */ |
| 1395 | i = i * 10LL; |
| 1396 | if (i - j > i) return 1; /* check for subtract overflow */ |
| 1397 | i = i - j; |
| 1398 | } |
| 1399 | } |
| 1400 | *ret = i; |
| 1401 | return 0; |
| 1402 | } |
| 1403 | |
Thierry FOURNIER | 511e947 | 2014-01-23 17:40:34 +0100 | [diff] [blame] | 1404 | /* This function is used with pat_parse_dotted_ver(). It converts a string |
| 1405 | * composed by two number separated by a dot. Each part must contain in 16 bits |
| 1406 | * because internally they will be represented as a 32-bit quantity stored in |
| 1407 | * a 64-bit integer. It returns zero when the number has successfully been |
| 1408 | * converted, non-zero otherwise. When an error is returned, the <ret> value |
| 1409 | * is left untouched. |
| 1410 | * |
| 1411 | * "1.3" -> 0x0000000000010003 |
| 1412 | * "65535.65535" -> 0x00000000ffffffff |
| 1413 | */ |
| 1414 | int strl2llrc_dotted(const char *text, int len, long long *ret) |
| 1415 | { |
| 1416 | const char *end = &text[len]; |
| 1417 | const char *p; |
| 1418 | long long major, minor; |
| 1419 | |
| 1420 | /* Look for dot. */ |
| 1421 | for (p = text; p < end; p++) |
| 1422 | if (*p == '.') |
| 1423 | break; |
| 1424 | |
| 1425 | /* Convert major. */ |
| 1426 | if (strl2llrc(text, p - text, &major) != 0) |
| 1427 | return 1; |
| 1428 | |
| 1429 | /* Check major. */ |
| 1430 | if (major >= 65536) |
| 1431 | return 1; |
| 1432 | |
| 1433 | /* Convert minor. */ |
| 1434 | minor = 0; |
| 1435 | if (p < end) |
| 1436 | if (strl2llrc(p + 1, end - (p + 1), &minor) != 0) |
| 1437 | return 1; |
| 1438 | |
| 1439 | /* Check minor. */ |
| 1440 | if (minor >= 65536) |
| 1441 | return 1; |
| 1442 | |
| 1443 | /* Compose value. */ |
| 1444 | *ret = (major << 16) | (minor & 0xffff); |
| 1445 | return 0; |
| 1446 | } |
| 1447 | |
Willy Tarreau | a0d37b6 | 2007-12-02 22:00:35 +0100 | [diff] [blame] | 1448 | /* This function parses a time value optionally followed by a unit suffix among |
| 1449 | * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit |
| 1450 | * expected by the caller. The computation does its best to avoid overflows. |
| 1451 | * The value is returned in <ret> if everything is fine, and a NULL is returned |
| 1452 | * by the function. In case of error, a pointer to the error is returned and |
| 1453 | * <ret> is left untouched. Values are automatically rounded up when needed. |
| 1454 | */ |
| 1455 | const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags) |
| 1456 | { |
| 1457 | unsigned imult, idiv; |
| 1458 | unsigned omult, odiv; |
| 1459 | unsigned value; |
| 1460 | |
| 1461 | omult = odiv = 1; |
| 1462 | |
| 1463 | switch (unit_flags & TIME_UNIT_MASK) { |
| 1464 | case TIME_UNIT_US: omult = 1000000; break; |
| 1465 | case TIME_UNIT_MS: omult = 1000; break; |
| 1466 | case TIME_UNIT_S: break; |
| 1467 | case TIME_UNIT_MIN: odiv = 60; break; |
| 1468 | case TIME_UNIT_HOUR: odiv = 3600; break; |
| 1469 | case TIME_UNIT_DAY: odiv = 86400; break; |
| 1470 | default: break; |
| 1471 | } |
| 1472 | |
| 1473 | value = 0; |
| 1474 | |
| 1475 | while (1) { |
| 1476 | unsigned int j; |
| 1477 | |
| 1478 | j = *text - '0'; |
| 1479 | if (j > 9) |
| 1480 | break; |
| 1481 | text++; |
| 1482 | value *= 10; |
| 1483 | value += j; |
| 1484 | } |
| 1485 | |
| 1486 | imult = idiv = 1; |
| 1487 | switch (*text) { |
| 1488 | case '\0': /* no unit = default unit */ |
| 1489 | imult = omult = idiv = odiv = 1; |
| 1490 | break; |
| 1491 | case 's': /* second = unscaled unit */ |
| 1492 | break; |
| 1493 | case 'u': /* microsecond : "us" */ |
| 1494 | if (text[1] == 's') { |
| 1495 | idiv = 1000000; |
| 1496 | text++; |
| 1497 | } |
| 1498 | break; |
| 1499 | case 'm': /* millisecond : "ms" or minute: "m" */ |
| 1500 | if (text[1] == 's') { |
| 1501 | idiv = 1000; |
| 1502 | text++; |
| 1503 | } else |
| 1504 | imult = 60; |
| 1505 | break; |
| 1506 | case 'h': /* hour : "h" */ |
| 1507 | imult = 3600; |
| 1508 | break; |
| 1509 | case 'd': /* day : "d" */ |
| 1510 | imult = 86400; |
| 1511 | break; |
| 1512 | default: |
| 1513 | return text; |
| 1514 | break; |
| 1515 | } |
| 1516 | |
| 1517 | if (omult % idiv == 0) { omult /= idiv; idiv = 1; } |
| 1518 | if (idiv % omult == 0) { idiv /= omult; omult = 1; } |
| 1519 | if (imult % odiv == 0) { imult /= odiv; odiv = 1; } |
| 1520 | if (odiv % imult == 0) { odiv /= imult; imult = 1; } |
| 1521 | |
| 1522 | value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv); |
| 1523 | *ret = value; |
| 1524 | return NULL; |
| 1525 | } |
Willy Tarreau | 6911fa4 | 2007-03-04 18:06:08 +0100 | [diff] [blame] | 1526 | |
Emeric Brun | 39132b2 | 2010-01-04 14:57:24 +0100 | [diff] [blame] | 1527 | /* this function converts the string starting at <text> to an unsigned int |
| 1528 | * stored in <ret>. If an error is detected, the pointer to the unexpected |
| 1529 | * character is returned. If the conversio is succesful, NULL is returned. |
| 1530 | */ |
| 1531 | const char *parse_size_err(const char *text, unsigned *ret) { |
| 1532 | unsigned value = 0; |
| 1533 | |
| 1534 | while (1) { |
| 1535 | unsigned int j; |
| 1536 | |
| 1537 | j = *text - '0'; |
| 1538 | if (j > 9) |
| 1539 | break; |
| 1540 | if (value > ~0U / 10) |
| 1541 | return text; |
| 1542 | value *= 10; |
| 1543 | if (value > (value + j)) |
| 1544 | return text; |
| 1545 | value += j; |
| 1546 | text++; |
| 1547 | } |
| 1548 | |
| 1549 | switch (*text) { |
| 1550 | case '\0': |
| 1551 | break; |
| 1552 | case 'K': |
| 1553 | case 'k': |
| 1554 | if (value > ~0U >> 10) |
| 1555 | return text; |
| 1556 | value = value << 10; |
| 1557 | break; |
| 1558 | case 'M': |
| 1559 | case 'm': |
| 1560 | if (value > ~0U >> 20) |
| 1561 | return text; |
| 1562 | value = value << 20; |
| 1563 | break; |
| 1564 | case 'G': |
| 1565 | case 'g': |
| 1566 | if (value > ~0U >> 30) |
| 1567 | return text; |
| 1568 | value = value << 30; |
| 1569 | break; |
| 1570 | default: |
| 1571 | return text; |
| 1572 | } |
| 1573 | |
| 1574 | *ret = value; |
| 1575 | return NULL; |
| 1576 | } |
| 1577 | |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1578 | /* |
| 1579 | * Parse binary string written in hexadecimal (source) and store the decoded |
| 1580 | * result into binstr and set binstrlen to the lengh of binstr. Memory for |
| 1581 | * 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] | 1582 | * error message in err. In succes case, it returns the consumed length. |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1583 | */ |
| 1584 | int parse_binary(const char *source, char **binstr, int *binstrlen, char **err) |
| 1585 | { |
| 1586 | int len; |
| 1587 | const char *p = source; |
| 1588 | int i,j; |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 1589 | int alloc; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1590 | |
| 1591 | len = strlen(source); |
| 1592 | if (len % 2) { |
| 1593 | memprintf(err, "an even number of hex digit is expected"); |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | len = len >> 1; |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 1598 | |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1599 | if (!*binstr) { |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 1600 | *binstr = calloc(len, sizeof(char)); |
| 1601 | if (!*binstr) { |
| 1602 | memprintf(err, "out of memory while loading string pattern"); |
| 1603 | return 0; |
| 1604 | } |
| 1605 | alloc = 1; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1606 | } |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 1607 | else { |
| 1608 | if (*binstrlen < len) { |
| 1609 | memprintf(err, "no space avalaible in the buffer. expect %d, provides %d", |
| 1610 | len, *binstrlen); |
| 1611 | return 0; |
| 1612 | } |
| 1613 | alloc = 0; |
| 1614 | } |
| 1615 | *binstrlen = len; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1616 | |
| 1617 | i = j = 0; |
| 1618 | while (j < len) { |
| 1619 | if (!ishex(p[i++])) |
| 1620 | goto bad_input; |
| 1621 | if (!ishex(p[i++])) |
| 1622 | goto bad_input; |
| 1623 | (*binstr)[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]); |
| 1624 | } |
Thierry FOURNIER | ee330af | 2014-01-21 11:36:14 +0100 | [diff] [blame] | 1625 | return len << 1; |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1626 | |
| 1627 | bad_input: |
| 1628 | memprintf(err, "an hex digit is expected (found '%c')", p[i-1]); |
Thierry FOURNIER | 9645d42 | 2013-12-06 19:59:28 +0100 | [diff] [blame] | 1629 | if (alloc) |
| 1630 | free(binstr); |
Willy Tarreau | 126d406 | 2013-12-03 17:50:47 +0100 | [diff] [blame] | 1631 | return 0; |
| 1632 | } |
| 1633 | |
Willy Tarreau | 946ba59 | 2009-05-10 15:41:18 +0200 | [diff] [blame] | 1634 | /* copies at most <n> characters from <src> and always terminates with '\0' */ |
| 1635 | char *my_strndup(const char *src, int n) |
| 1636 | { |
| 1637 | int len = 0; |
| 1638 | char *ret; |
| 1639 | |
| 1640 | while (len < n && src[len]) |
| 1641 | len++; |
| 1642 | |
| 1643 | ret = (char *)malloc(len + 1); |
| 1644 | if (!ret) |
| 1645 | return ret; |
| 1646 | memcpy(ret, src, len); |
| 1647 | ret[len] = '\0'; |
| 1648 | return ret; |
| 1649 | } |
| 1650 | |
Baptiste Assmann | bb77c8e | 2013-10-06 23:24:13 +0200 | [diff] [blame] | 1651 | /* |
| 1652 | * search needle in haystack |
| 1653 | * returns the pointer if found, returns NULL otherwise |
| 1654 | */ |
| 1655 | const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) |
| 1656 | { |
| 1657 | const void *c = NULL; |
| 1658 | unsigned char f; |
| 1659 | |
| 1660 | if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen)) |
| 1661 | return NULL; |
| 1662 | |
| 1663 | f = *(char *)needle; |
| 1664 | c = haystack; |
| 1665 | while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) { |
| 1666 | if ((haystacklen - (c - haystack)) < needlelen) |
| 1667 | return NULL; |
| 1668 | |
| 1669 | if (memcmp(c, needle, needlelen) == 0) |
| 1670 | return c; |
| 1671 | ++c; |
| 1672 | } |
| 1673 | return NULL; |
| 1674 | } |
| 1675 | |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 1676 | /* This function returns the first unused key greater than or equal to <key> in |
| 1677 | * ID tree <root>. Zero is returned if no place is found. |
| 1678 | */ |
| 1679 | unsigned int get_next_id(struct eb_root *root, unsigned int key) |
| 1680 | { |
| 1681 | struct eb32_node *used; |
| 1682 | |
| 1683 | do { |
| 1684 | used = eb32_lookup_ge(root, key); |
| 1685 | if (!used || used->key > key) |
| 1686 | return key; /* key is available */ |
| 1687 | key++; |
| 1688 | } while (key); |
| 1689 | return key; |
| 1690 | } |
| 1691 | |
Willy Tarreau | 348238b | 2010-01-18 15:05:57 +0100 | [diff] [blame] | 1692 | /* This function compares a sample word possibly followed by blanks to another |
| 1693 | * clean word. The compare is case-insensitive. 1 is returned if both are equal, |
| 1694 | * otherwise zero. This intends to be used when checking HTTP headers for some |
| 1695 | * values. Note that it validates a word followed only by blanks but does not |
| 1696 | * validate a word followed by blanks then other chars. |
| 1697 | */ |
| 1698 | int word_match(const char *sample, int slen, const char *word, int wlen) |
| 1699 | { |
| 1700 | if (slen < wlen) |
| 1701 | return 0; |
| 1702 | |
| 1703 | while (wlen) { |
| 1704 | char c = *sample ^ *word; |
| 1705 | if (c && c != ('A' ^ 'a')) |
| 1706 | return 0; |
| 1707 | sample++; |
| 1708 | word++; |
| 1709 | slen--; |
| 1710 | wlen--; |
| 1711 | } |
| 1712 | |
| 1713 | while (slen) { |
| 1714 | if (*sample != ' ' && *sample != '\t') |
| 1715 | return 0; |
| 1716 | sample++; |
| 1717 | slen--; |
| 1718 | } |
| 1719 | return 1; |
| 1720 | } |
Willy Tarreau | 482b00d | 2009-10-04 22:48:42 +0200 | [diff] [blame] | 1721 | |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 1722 | /* Converts any text-formatted IPv4 address to a host-order IPv4 address. It |
| 1723 | * is particularly fast because it avoids expensive operations such as |
| 1724 | * multiplies, which are optimized away at the end. It requires a properly |
| 1725 | * formated address though (3 points). |
| 1726 | */ |
| 1727 | unsigned int inetaddr_host(const char *text) |
| 1728 | { |
| 1729 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 1730 | register unsigned int dig100, dig10, dig1; |
| 1731 | int s; |
| 1732 | const char *p, *d; |
| 1733 | |
| 1734 | dig1 = dig10 = dig100 = ascii_zero; |
| 1735 | s = 24; |
| 1736 | |
| 1737 | p = text; |
| 1738 | while (1) { |
| 1739 | if (((unsigned)(*p - '0')) <= 9) { |
| 1740 | p++; |
| 1741 | continue; |
| 1742 | } |
| 1743 | |
| 1744 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1745 | if (p == text) |
| 1746 | goto end; |
| 1747 | |
| 1748 | d = p - 1; |
| 1749 | dig1 |= (unsigned int)(*d << s); |
| 1750 | if (d == text) |
| 1751 | goto end; |
| 1752 | |
| 1753 | d--; |
| 1754 | dig10 |= (unsigned int)(*d << s); |
| 1755 | if (d == text) |
| 1756 | goto end; |
| 1757 | |
| 1758 | d--; |
| 1759 | dig100 |= (unsigned int)(*d << s); |
| 1760 | end: |
| 1761 | if (!s || *p != '.') |
| 1762 | break; |
| 1763 | |
| 1764 | s -= 8; |
| 1765 | text = ++p; |
| 1766 | } |
| 1767 | |
| 1768 | dig100 -= ascii_zero; |
| 1769 | dig10 -= ascii_zero; |
| 1770 | dig1 -= ascii_zero; |
| 1771 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1772 | } |
| 1773 | |
| 1774 | /* |
| 1775 | * Idem except the first unparsed character has to be passed in <stop>. |
| 1776 | */ |
| 1777 | unsigned int inetaddr_host_lim(const char *text, const char *stop) |
| 1778 | { |
| 1779 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 1780 | register unsigned int dig100, dig10, dig1; |
| 1781 | int s; |
| 1782 | const char *p, *d; |
| 1783 | |
| 1784 | dig1 = dig10 = dig100 = ascii_zero; |
| 1785 | s = 24; |
| 1786 | |
| 1787 | p = text; |
| 1788 | while (1) { |
| 1789 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 1790 | p++; |
| 1791 | continue; |
| 1792 | } |
| 1793 | |
| 1794 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1795 | if (p == text) |
| 1796 | goto end; |
| 1797 | |
| 1798 | d = p - 1; |
| 1799 | dig1 |= (unsigned int)(*d << s); |
| 1800 | if (d == text) |
| 1801 | goto end; |
| 1802 | |
| 1803 | d--; |
| 1804 | dig10 |= (unsigned int)(*d << s); |
| 1805 | if (d == text) |
| 1806 | goto end; |
| 1807 | |
| 1808 | d--; |
| 1809 | dig100 |= (unsigned int)(*d << s); |
| 1810 | end: |
| 1811 | if (!s || p == stop || *p != '.') |
| 1812 | break; |
| 1813 | |
| 1814 | s -= 8; |
| 1815 | text = ++p; |
| 1816 | } |
| 1817 | |
| 1818 | dig100 -= ascii_zero; |
| 1819 | dig10 -= ascii_zero; |
| 1820 | dig1 -= ascii_zero; |
| 1821 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1822 | } |
| 1823 | |
| 1824 | /* |
| 1825 | * Idem except the pointer to first unparsed byte is returned into <ret> which |
| 1826 | * must not be NULL. |
| 1827 | */ |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 1828 | unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret) |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 1829 | { |
| 1830 | const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0'; |
| 1831 | register unsigned int dig100, dig10, dig1; |
| 1832 | int s; |
Willy Tarreau | 7417275 | 2010-10-15 23:21:42 +0200 | [diff] [blame] | 1833 | char *p, *d; |
Willy Tarreau | d54bbdc | 2009-09-07 11:00:31 +0200 | [diff] [blame] | 1834 | |
| 1835 | dig1 = dig10 = dig100 = ascii_zero; |
| 1836 | s = 24; |
| 1837 | |
| 1838 | p = text; |
| 1839 | while (1) { |
| 1840 | if (((unsigned)(*p - '0')) <= 9 && p < stop) { |
| 1841 | p++; |
| 1842 | continue; |
| 1843 | } |
| 1844 | |
| 1845 | /* here, we have a complete byte between <text> and <p> (exclusive) */ |
| 1846 | if (p == text) |
| 1847 | goto end; |
| 1848 | |
| 1849 | d = p - 1; |
| 1850 | dig1 |= (unsigned int)(*d << s); |
| 1851 | if (d == text) |
| 1852 | goto end; |
| 1853 | |
| 1854 | d--; |
| 1855 | dig10 |= (unsigned int)(*d << s); |
| 1856 | if (d == text) |
| 1857 | goto end; |
| 1858 | |
| 1859 | d--; |
| 1860 | dig100 |= (unsigned int)(*d << s); |
| 1861 | end: |
| 1862 | if (!s || p == stop || *p != '.') |
| 1863 | break; |
| 1864 | |
| 1865 | s -= 8; |
| 1866 | text = ++p; |
| 1867 | } |
| 1868 | |
| 1869 | *ret = p; |
| 1870 | dig100 -= ascii_zero; |
| 1871 | dig10 -= ascii_zero; |
| 1872 | dig1 -= ascii_zero; |
| 1873 | return ((dig100 * 10) + dig10) * 10 + dig1; |
| 1874 | } |
| 1875 | |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 1876 | /* Convert a fixed-length string to an IP address. Returns 0 in case of error, |
| 1877 | * or the number of chars read in case of success. Maybe this could be replaced |
| 1878 | * by one of the functions above. Also, apparently this function does not support |
| 1879 | * hosts above 255 and requires exactly 4 octets. |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 1880 | * The destination is only modified on success. |
Willy Tarreau | f0b38bf | 2010-06-06 13:22:23 +0200 | [diff] [blame] | 1881 | */ |
| 1882 | int buf2ip(const char *buf, size_t len, struct in_addr *dst) |
| 1883 | { |
| 1884 | const char *addr; |
| 1885 | int saw_digit, octets, ch; |
| 1886 | u_char tmp[4], *tp; |
| 1887 | const char *cp = buf; |
| 1888 | |
| 1889 | saw_digit = 0; |
| 1890 | octets = 0; |
| 1891 | *(tp = tmp) = 0; |
| 1892 | |
| 1893 | for (addr = buf; addr - buf < len; addr++) { |
| 1894 | unsigned char digit = (ch = *addr) - '0'; |
| 1895 | |
| 1896 | if (digit > 9 && ch != '.') |
| 1897 | break; |
| 1898 | |
| 1899 | if (digit <= 9) { |
| 1900 | u_int new = *tp * 10 + digit; |
| 1901 | |
| 1902 | if (new > 255) |
| 1903 | return 0; |
| 1904 | |
| 1905 | *tp = new; |
| 1906 | |
| 1907 | if (!saw_digit) { |
| 1908 | if (++octets > 4) |
| 1909 | return 0; |
| 1910 | saw_digit = 1; |
| 1911 | } |
| 1912 | } else if (ch == '.' && saw_digit) { |
| 1913 | if (octets == 4) |
| 1914 | return 0; |
| 1915 | |
| 1916 | *++tp = 0; |
| 1917 | saw_digit = 0; |
| 1918 | } else |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
| 1922 | if (octets < 4) |
| 1923 | return 0; |
| 1924 | |
| 1925 | memcpy(&dst->s_addr, tmp, 4); |
| 1926 | return addr - cp; |
| 1927 | } |
| 1928 | |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1929 | /* This function converts the string in <buf> of the len <len> to |
| 1930 | * struct in6_addr <dst> which must be allocated by the caller. |
| 1931 | * This function returns 1 in success case, otherwise zero. |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 1932 | * The destination is only modified on success. |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1933 | */ |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1934 | int buf2ip6(const char *buf, size_t len, struct in6_addr *dst) |
| 1935 | { |
Thierry FOURNIER | cd65991 | 2013-12-11 12:33:54 +0100 | [diff] [blame] | 1936 | char null_term_ip6[INET6_ADDRSTRLEN + 1]; |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 1937 | struct in6_addr out; |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1938 | |
Thierry FOURNIER | cd65991 | 2013-12-11 12:33:54 +0100 | [diff] [blame] | 1939 | if (len > INET6_ADDRSTRLEN) |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1940 | return 0; |
| 1941 | |
| 1942 | memcpy(null_term_ip6, buf, len); |
| 1943 | null_term_ip6[len] = '\0'; |
| 1944 | |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 1945 | if (!inet_pton(AF_INET6, null_term_ip6, &out)) |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1946 | return 0; |
| 1947 | |
Willy Tarreau | 075415a | 2013-12-12 11:29:39 +0100 | [diff] [blame] | 1948 | *dst = out; |
Thierry FOURNIER | d559dd8 | 2013-11-22 16:16:59 +0100 | [diff] [blame] | 1949 | return 1; |
| 1950 | } |
| 1951 | |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 1952 | /* To be used to quote config arg positions. Returns the short string at <ptr> |
| 1953 | * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line" |
| 1954 | * if ptr is NULL or empty. The string is locally allocated. |
| 1955 | */ |
| 1956 | const char *quote_arg(const char *ptr) |
| 1957 | { |
| 1958 | static char val[32]; |
| 1959 | int i; |
| 1960 | |
| 1961 | if (!ptr || !*ptr) |
| 1962 | return "end of line"; |
| 1963 | val[0] = '\''; |
Willy Tarreau | de2dd6b | 2013-01-24 02:14:42 +0100 | [diff] [blame] | 1964 | for (i = 1; i < sizeof(val) - 2 && *ptr; i++) |
Willy Tarreau | acf9577 | 2010-06-14 19:09:21 +0200 | [diff] [blame] | 1965 | val[i] = *ptr++; |
| 1966 | val[i++] = '\''; |
| 1967 | val[i] = '\0'; |
| 1968 | return val; |
| 1969 | } |
| 1970 | |
Willy Tarreau | 5b18020 | 2010-07-18 10:40:48 +0200 | [diff] [blame] | 1971 | /* returns an operator among STD_OP_* for string <str> or < 0 if unknown */ |
| 1972 | int get_std_op(const char *str) |
| 1973 | { |
| 1974 | int ret = -1; |
| 1975 | |
| 1976 | if (*str == 'e' && str[1] == 'q') |
| 1977 | ret = STD_OP_EQ; |
| 1978 | else if (*str == 'n' && str[1] == 'e') |
| 1979 | ret = STD_OP_NE; |
| 1980 | else if (*str == 'l') { |
| 1981 | if (str[1] == 'e') ret = STD_OP_LE; |
| 1982 | else if (str[1] == 't') ret = STD_OP_LT; |
| 1983 | } |
| 1984 | else if (*str == 'g') { |
| 1985 | if (str[1] == 'e') ret = STD_OP_GE; |
| 1986 | else if (str[1] == 't') ret = STD_OP_GT; |
| 1987 | } |
| 1988 | |
| 1989 | if (ret == -1 || str[2] != '\0') |
| 1990 | return -1; |
| 1991 | return ret; |
| 1992 | } |
| 1993 | |
Willy Tarreau | 4c14eaa | 2010-11-24 14:01:45 +0100 | [diff] [blame] | 1994 | /* hash a 32-bit integer to another 32-bit integer */ |
| 1995 | unsigned int full_hash(unsigned int a) |
| 1996 | { |
| 1997 | return __full_hash(a); |
| 1998 | } |
| 1999 | |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 2000 | /* Return non-zero if IPv4 address is part of the network, |
| 2001 | * otherwise zero. |
| 2002 | */ |
| 2003 | int in_net_ipv4(struct in_addr *addr, struct in_addr *mask, struct in_addr *net) |
| 2004 | { |
| 2005 | return((addr->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr)); |
| 2006 | } |
| 2007 | |
| 2008 | /* Return non-zero if IPv6 address is part of the network, |
| 2009 | * otherwise zero. |
| 2010 | */ |
| 2011 | int in_net_ipv6(struct in6_addr *addr, struct in6_addr *mask, struct in6_addr *net) |
| 2012 | { |
| 2013 | int i; |
| 2014 | |
| 2015 | for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++) |
| 2016 | if (((((int *)addr)[i] & ((int *)mask)[i])) != |
| 2017 | (((int *)net)[i] & ((int *)mask)[i])) |
| 2018 | return 0; |
| 2019 | return 1; |
| 2020 | } |
| 2021 | |
| 2022 | /* RFC 4291 prefix */ |
| 2023 | const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00, |
| 2024 | 0x00, 0x00, 0x00, 0x00, |
| 2025 | 0x00, 0x00, 0xFF, 0xFF }; |
| 2026 | |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 2027 | /* Map IPv4 adress on IPv6 address, as specified in RFC 3513. |
| 2028 | * Input and output may overlap. |
| 2029 | */ |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 2030 | void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr) |
| 2031 | { |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 2032 | struct in_addr tmp_addr; |
| 2033 | |
| 2034 | tmp_addr.s_addr = sin_addr->s_addr; |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 2035 | memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)); |
Thierry FOURNIER | 4a04dc3 | 2013-11-28 16:33:15 +0100 | [diff] [blame] | 2036 | memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4); |
David du Colombier | 4f92d32 | 2011-03-24 11:09:31 +0100 | [diff] [blame] | 2037 | } |
| 2038 | |
| 2039 | /* Map IPv6 adress on IPv4 address, as specified in RFC 3513. |
| 2040 | * Return true if conversion is possible and false otherwise. |
| 2041 | */ |
| 2042 | int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr) |
| 2043 | { |
| 2044 | if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) { |
| 2045 | memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]), |
| 2046 | sizeof(struct in_addr)); |
| 2047 | return 1; |
| 2048 | } |
| 2049 | |
| 2050 | return 0; |
| 2051 | } |
| 2052 | |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2053 | char *human_time(int t, short hz_div) { |
| 2054 | static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s" |
| 2055 | char *p = rv; |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2056 | char *end = rv + sizeof(rv); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2057 | int cnt=2; // print two numbers |
| 2058 | |
| 2059 | if (unlikely(t < 0 || hz_div <= 0)) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2060 | snprintf(p, end - p, "?"); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2061 | return rv; |
| 2062 | } |
| 2063 | |
| 2064 | if (unlikely(hz_div > 1)) |
| 2065 | t /= hz_div; |
| 2066 | |
| 2067 | if (t >= DAY) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2068 | p += snprintf(p, end - p, "%dd", t / DAY); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2069 | cnt--; |
| 2070 | } |
| 2071 | |
| 2072 | if (cnt && t % DAY / HOUR) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2073 | p += snprintf(p, end - p, "%dh", t % DAY / HOUR); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2074 | cnt--; |
| 2075 | } |
| 2076 | |
| 2077 | if (cnt && t % HOUR / MINUTE) { |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2078 | p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2079 | cnt--; |
| 2080 | } |
| 2081 | |
| 2082 | if ((cnt && t % MINUTE) || !t) // also display '0s' |
Willy Tarreau | 761b3d5 | 2014-04-14 14:53:06 +0200 | [diff] [blame] | 2083 | p += snprintf(p, end - p, "%ds", t % MINUTE / SEC); |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2084 | |
| 2085 | return rv; |
| 2086 | } |
| 2087 | |
| 2088 | const char *monthname[12] = { |
| 2089 | "Jan", "Feb", "Mar", "Apr", "May", "Jun", |
| 2090 | "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" |
| 2091 | }; |
| 2092 | |
| 2093 | /* date2str_log: write a date in the format : |
| 2094 | * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d", |
| 2095 | * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900, |
| 2096 | * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000); |
| 2097 | * |
| 2098 | * without using sprintf. return a pointer to the last char written (\0) or |
| 2099 | * NULL if there isn't enough space. |
| 2100 | */ |
| 2101 | char *date2str_log(char *dst, struct tm *tm, struct timeval *date, size_t size) |
| 2102 | { |
| 2103 | |
| 2104 | if (size < 25) /* the size is fixed: 24 chars + \0 */ |
| 2105 | return NULL; |
| 2106 | |
| 2107 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
| 2108 | *dst++ = '/'; |
| 2109 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 2110 | dst += 3; |
| 2111 | *dst++ = '/'; |
| 2112 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
| 2113 | *dst++ = ':'; |
| 2114 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
| 2115 | *dst++ = ':'; |
| 2116 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
| 2117 | *dst++ = ':'; |
| 2118 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
| 2119 | *dst++ = '.'; |
| 2120 | utoa_pad((unsigned int)(date->tv_usec/1000), dst, 4); // millisecondes |
| 2121 | dst += 3; // only the 3 first digits |
| 2122 | *dst = '\0'; |
| 2123 | |
| 2124 | return dst; |
| 2125 | } |
| 2126 | |
| 2127 | /* gmt2str_log: write a date in the format : |
| 2128 | * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf |
| 2129 | * return a pointer to the last char written (\0) or |
| 2130 | * NULL if there isn't enough space. |
| 2131 | */ |
| 2132 | char *gmt2str_log(char *dst, struct tm *tm, size_t size) |
| 2133 | { |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 2134 | if (size < 27) /* the size is fixed: 26 chars + \0 */ |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2135 | return NULL; |
| 2136 | |
| 2137 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
| 2138 | *dst++ = '/'; |
| 2139 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 2140 | dst += 3; |
| 2141 | *dst++ = '/'; |
| 2142 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
| 2143 | *dst++ = ':'; |
| 2144 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
| 2145 | *dst++ = ':'; |
| 2146 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
| 2147 | *dst++ = ':'; |
| 2148 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
| 2149 | *dst++ = ' '; |
| 2150 | *dst++ = '+'; |
| 2151 | *dst++ = '0'; |
| 2152 | *dst++ = '0'; |
| 2153 | *dst++ = '0'; |
| 2154 | *dst++ = '0'; |
| 2155 | *dst = '\0'; |
| 2156 | |
| 2157 | return dst; |
| 2158 | } |
| 2159 | |
Yuxans Yao | 4e25b01 | 2012-10-19 10:36:09 +0800 | [diff] [blame] | 2160 | /* localdate2str_log: write a date in the format : |
| 2161 | * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf |
| 2162 | * * return a pointer to the last char written (\0) or |
| 2163 | * * NULL if there isn't enough space. |
| 2164 | */ |
| 2165 | char *localdate2str_log(char *dst, struct tm *tm, size_t size) |
| 2166 | { |
| 2167 | if (size < 27) /* the size is fixed: 26 chars + \0 */ |
| 2168 | return NULL; |
| 2169 | |
| 2170 | dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day |
| 2171 | *dst++ = '/'; |
| 2172 | memcpy(dst, monthname[tm->tm_mon], 3); // month |
| 2173 | dst += 3; |
| 2174 | *dst++ = '/'; |
| 2175 | dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year |
| 2176 | *dst++ = ':'; |
| 2177 | dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour |
| 2178 | *dst++ = ':'; |
| 2179 | dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes |
| 2180 | *dst++ = ':'; |
| 2181 | dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes |
| 2182 | *dst++ = ' '; |
| 2183 | memcpy(dst, localtimezone, 5); // timezone |
| 2184 | dst += 5; |
| 2185 | *dst = '\0'; |
| 2186 | |
| 2187 | return dst; |
| 2188 | } |
| 2189 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 2190 | /* Dynamically allocates a string of the proper length to hold the formatted |
| 2191 | * output. NULL is returned on error. The caller is responsible for freeing the |
| 2192 | * memory area using free(). The resulting string is returned in <out> if the |
| 2193 | * pointer is not NULL. A previous version of <out> might be used to build the |
| 2194 | * new string, and it will be freed before returning if it is not NULL, which |
| 2195 | * makes it possible to build complex strings from iterative calls without |
| 2196 | * having to care about freeing intermediate values, as in the example below : |
| 2197 | * |
| 2198 | * memprintf(&err, "invalid argument: '%s'", arg); |
| 2199 | * ... |
| 2200 | * memprintf(&err, "parser said : <%s>\n", *err); |
| 2201 | * ... |
| 2202 | * free(*err); |
| 2203 | * |
| 2204 | * This means that <err> must be initialized to NULL before first invocation. |
| 2205 | * The return value also holds the allocated string, which eases error checking |
| 2206 | * 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] | 2207 | * passed instead and it will be ignored. The returned message will then also |
| 2208 | * 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] | 2209 | * |
| 2210 | * It is also convenient to use it without any free except the last one : |
| 2211 | * err = NULL; |
| 2212 | * if (!fct1(err)) report(*err); |
| 2213 | * if (!fct2(err)) report(*err); |
| 2214 | * if (!fct3(err)) report(*err); |
| 2215 | * free(*err); |
| 2216 | */ |
| 2217 | char *memprintf(char **out, const char *format, ...) |
| 2218 | { |
| 2219 | va_list args; |
| 2220 | char *ret = NULL; |
| 2221 | int allocated = 0; |
| 2222 | int needed = 0; |
| 2223 | |
Willy Tarreau | eb6cead | 2012-09-20 19:43:14 +0200 | [diff] [blame] | 2224 | if (!out) |
| 2225 | return NULL; |
| 2226 | |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 2227 | do { |
| 2228 | /* vsnprintf() will return the required length even when the |
| 2229 | * target buffer is NULL. We do this in a loop just in case |
| 2230 | * intermediate evaluations get wrong. |
| 2231 | */ |
| 2232 | va_start(args, format); |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 2233 | needed = vsnprintf(ret, allocated, format, args); |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 2234 | va_end(args); |
| 2235 | |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 2236 | if (needed < allocated) { |
| 2237 | /* Note: on Solaris 8, the first iteration always |
| 2238 | * returns -1 if allocated is zero, so we force a |
| 2239 | * retry. |
| 2240 | */ |
| 2241 | if (!allocated) |
| 2242 | needed = 0; |
| 2243 | else |
| 2244 | break; |
| 2245 | } |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 2246 | |
Willy Tarreau | 1b2fed6 | 2013-04-01 22:48:54 +0200 | [diff] [blame] | 2247 | allocated = needed + 1; |
Willy Tarreau | 9a7bea5 | 2012-04-27 11:16:50 +0200 | [diff] [blame] | 2248 | ret = realloc(ret, allocated); |
| 2249 | } while (ret); |
| 2250 | |
| 2251 | if (needed < 0) { |
| 2252 | /* an error was encountered */ |
| 2253 | free(ret); |
| 2254 | ret = NULL; |
| 2255 | } |
| 2256 | |
| 2257 | if (out) { |
| 2258 | free(*out); |
| 2259 | *out = ret; |
| 2260 | } |
| 2261 | |
| 2262 | return ret; |
| 2263 | } |
William Lallemand | 421f5b5 | 2012-02-06 18:15:57 +0100 | [diff] [blame] | 2264 | |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 2265 | /* Used to add <level> spaces before each line of <out>, unless there is only one line. |
| 2266 | * 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] | 2267 | * freed by the caller. It also supports being passed a NULL which results in the same |
| 2268 | * output. |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 2269 | * Example of use : |
| 2270 | * parse(cmd, &err); (callee: memprintf(&err, ...)) |
| 2271 | * fprintf(stderr, "Parser said: %s\n", indent_error(&err)); |
| 2272 | * free(err); |
| 2273 | */ |
| 2274 | char *indent_msg(char **out, int level) |
| 2275 | { |
| 2276 | char *ret, *in, *p; |
| 2277 | int needed = 0; |
| 2278 | int lf = 0; |
| 2279 | int lastlf = 0; |
| 2280 | int len; |
| 2281 | |
Willy Tarreau | 70eec38 | 2012-10-10 08:56:47 +0200 | [diff] [blame] | 2282 | if (!out || !*out) |
| 2283 | return NULL; |
| 2284 | |
Willy Tarreau | 21c705b | 2012-09-14 11:40:36 +0200 | [diff] [blame] | 2285 | in = *out - 1; |
| 2286 | while ((in = strchr(in + 1, '\n')) != NULL) { |
| 2287 | lastlf = in - *out; |
| 2288 | lf++; |
| 2289 | } |
| 2290 | |
| 2291 | if (!lf) /* single line, no LF, return it as-is */ |
| 2292 | return *out; |
| 2293 | |
| 2294 | len = strlen(*out); |
| 2295 | |
| 2296 | if (lf == 1 && lastlf == len - 1) { |
| 2297 | /* single line, LF at end, strip it and return as-is */ |
| 2298 | (*out)[lastlf] = 0; |
| 2299 | return *out; |
| 2300 | } |
| 2301 | |
| 2302 | /* OK now we have at least one LF, we need to process the whole string |
| 2303 | * as a multi-line string. What we'll do : |
| 2304 | * - prefix with an LF if there is none |
| 2305 | * - add <level> spaces before each line |
| 2306 | * This means at most ( 1 + level + (len-lf) + lf*<1+level) ) = |
| 2307 | * 1 + level + len + lf * level = 1 + level * (lf + 1) + len. |
| 2308 | */ |
| 2309 | |
| 2310 | needed = 1 + level * (lf + 1) + len + 1; |
| 2311 | p = ret = malloc(needed); |
| 2312 | in = *out; |
| 2313 | |
| 2314 | /* skip initial LFs */ |
| 2315 | while (*in == '\n') |
| 2316 | in++; |
| 2317 | |
| 2318 | /* copy each line, prefixed with LF and <level> spaces, and without the trailing LF */ |
| 2319 | while (*in) { |
| 2320 | *p++ = '\n'; |
| 2321 | memset(p, ' ', level); |
| 2322 | p += level; |
| 2323 | do { |
| 2324 | *p++ = *in++; |
| 2325 | } while (*in && *in != '\n'); |
| 2326 | if (*in) |
| 2327 | in++; |
| 2328 | } |
| 2329 | *p = 0; |
| 2330 | |
| 2331 | free(*out); |
| 2332 | *out = ret; |
| 2333 | |
| 2334 | return ret; |
| 2335 | } |
| 2336 | |
Willy Tarreau | dad36a3 | 2013-03-11 01:20:04 +0100 | [diff] [blame] | 2337 | /* Convert occurrences of environment variables in the input string to their |
| 2338 | * corresponding value. A variable is identified as a series of alphanumeric |
| 2339 | * characters or underscores following a '$' sign. The <in> string must be |
| 2340 | * free()able. NULL returns NULL. The resulting string might be reallocated if |
| 2341 | * some expansion is made. Variable names may also be enclosed into braces if |
| 2342 | * needed (eg: to concatenate alphanum characters). |
| 2343 | */ |
| 2344 | char *env_expand(char *in) |
| 2345 | { |
| 2346 | char *txt_beg; |
| 2347 | char *out; |
| 2348 | char *txt_end; |
| 2349 | char *var_beg; |
| 2350 | char *var_end; |
| 2351 | char *value; |
| 2352 | char *next; |
| 2353 | int out_len; |
| 2354 | int val_len; |
| 2355 | |
| 2356 | if (!in) |
| 2357 | return in; |
| 2358 | |
| 2359 | value = out = NULL; |
| 2360 | out_len = 0; |
| 2361 | |
| 2362 | txt_beg = in; |
| 2363 | do { |
| 2364 | /* look for next '$' sign in <in> */ |
| 2365 | for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++); |
| 2366 | |
| 2367 | if (!*txt_end && !out) /* end and no expansion performed */ |
| 2368 | return in; |
| 2369 | |
| 2370 | val_len = 0; |
| 2371 | next = txt_end; |
| 2372 | if (*txt_end == '$') { |
| 2373 | char save; |
| 2374 | |
| 2375 | var_beg = txt_end + 1; |
| 2376 | if (*var_beg == '{') |
| 2377 | var_beg++; |
| 2378 | |
| 2379 | var_end = var_beg; |
| 2380 | while (isalnum((int)(unsigned char)*var_end) || *var_end == '_') { |
| 2381 | var_end++; |
| 2382 | } |
| 2383 | |
| 2384 | next = var_end; |
| 2385 | if (*var_end == '}' && (var_beg > txt_end + 1)) |
| 2386 | next++; |
| 2387 | |
| 2388 | /* get value of the variable name at this location */ |
| 2389 | save = *var_end; |
| 2390 | *var_end = '\0'; |
| 2391 | value = getenv(var_beg); |
| 2392 | *var_end = save; |
| 2393 | val_len = value ? strlen(value) : 0; |
| 2394 | } |
| 2395 | |
| 2396 | out = realloc(out, out_len + (txt_end - txt_beg) + val_len + 1); |
| 2397 | if (txt_end > txt_beg) { |
| 2398 | memcpy(out + out_len, txt_beg, txt_end - txt_beg); |
| 2399 | out_len += txt_end - txt_beg; |
| 2400 | } |
| 2401 | if (val_len) { |
| 2402 | memcpy(out + out_len, value, val_len); |
| 2403 | out_len += val_len; |
| 2404 | } |
| 2405 | out[out_len] = 0; |
| 2406 | txt_beg = next; |
| 2407 | } while (*txt_beg); |
| 2408 | |
| 2409 | /* here we know that <out> was allocated and that we don't need <in> anymore */ |
| 2410 | free(in); |
| 2411 | return out; |
| 2412 | } |
| 2413 | |
de Lafond Guillaume | 88c278f | 2013-04-15 19:27:10 +0200 | [diff] [blame] | 2414 | |
| 2415 | /* same as strstr() but case-insensitive and with limit length */ |
| 2416 | const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2) |
| 2417 | { |
| 2418 | char *pptr, *sptr, *start; |
| 2419 | uint slen, plen; |
| 2420 | uint tmp1, tmp2; |
| 2421 | |
| 2422 | if (str1 == NULL || len_str1 == 0) // search pattern into an empty string => search is not found |
| 2423 | return NULL; |
| 2424 | |
| 2425 | if (str2 == NULL || len_str2 == 0) // pattern is empty => every str1 match |
| 2426 | return str1; |
| 2427 | |
| 2428 | if (len_str1 < len_str2) // pattern is longer than string => search is not found |
| 2429 | return NULL; |
| 2430 | |
| 2431 | for (tmp1 = 0, start = (char *)str1, pptr = (char *)str2, slen = len_str1, plen = len_str2; slen >= plen; start++, slen--) { |
| 2432 | while (toupper(*start) != toupper(*str2)) { |
| 2433 | start++; |
| 2434 | slen--; |
| 2435 | tmp1++; |
| 2436 | |
| 2437 | if (tmp1 >= len_str1) |
| 2438 | return NULL; |
| 2439 | |
| 2440 | /* if pattern longer than string */ |
| 2441 | if (slen < plen) |
| 2442 | return NULL; |
| 2443 | } |
| 2444 | |
| 2445 | sptr = start; |
| 2446 | pptr = (char *)str2; |
| 2447 | |
| 2448 | tmp2 = 0; |
| 2449 | while (toupper(*sptr) == toupper(*pptr)) { |
| 2450 | sptr++; |
| 2451 | pptr++; |
| 2452 | tmp2++; |
| 2453 | |
| 2454 | if (*pptr == '\0' || tmp2 == len_str2) /* end of pattern found */ |
| 2455 | return start; |
| 2456 | if (*sptr == '\0' || tmp2 == len_str1) /* end of string found and the pattern is not fully found */ |
| 2457 | return NULL; |
| 2458 | } |
| 2459 | } |
| 2460 | return NULL; |
| 2461 | } |
| 2462 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 2463 | /* |
| 2464 | * Local variables: |
| 2465 | * c-indent-level: 8 |
| 2466 | * c-basic-offset: 8 |
| 2467 | * End: |
| 2468 | */ |