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