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