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