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