blob: fb24f7d37cc8190baf12d1cfd002ac43b32554d4 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General purpose functions.
3 *
Willy Tarreau348238b2010-01-18 15:05:57 +01004 * Copyright 2000-2010 Willy Tarreau <w@1wt.eu>
Willy Tarreaubaaee002006-06-26 02:48:02 +02005 *
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 Tarreau2e74c3f2007-12-02 18:45:09 +010013#include <ctype.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020014#include <netdb.h>
Willy Tarreau9a7bea52012-04-27 11:16:50 +020015#include <stdarg.h>
Willy Tarreaudd2f85e2012-09-02 22:34:23 +020016#include <stdio.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020017#include <stdlib.h>
18#include <string.h>
Willy Tarreau127f9662007-12-06 00:53:51 +010019#include <sys/socket.h>
20#include <sys/un.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <netinet/in.h>
22#include <arpa/inet.h>
23
Thierry FOURNIERe059ec92014-03-17 12:01:13 +010024#include <common/chunk.h>
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020025#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020026#include <common/standard.h>
Thierry FOURNIER9f95e402014-03-21 14:51:46 +010027#include <types/global.h>
Baptiste Assmanna68ca962015-04-14 01:15:08 +020028#include <proto/dns.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010029#include <eb32tree.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020030
Willy Tarreau56adcf22012-12-23 18:00:29 +010031/* enough to store NB_ITOA_STR integers of :
Willy Tarreau72d759c2007-10-25 12:14:10 +020032 * 2^64-1 = 18446744073709551615 or
33 * -2^63 = -9223372036854775808
Willy Tarreaue7239b52009-03-29 13:41:58 +020034 *
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 Tarreau72d759c2007-10-25 12:14:10 +020038 */
Willy Tarreau56adcf22012-12-23 18:00:29 +010039char itoa_str[NB_ITOA_STR][171];
40int itoa_idx = 0; /* index of next itoa_str to use */
Willy Tarreaubaaee002006-06-26 02:48:02 +020041
Willy Tarreau588297f2014-06-16 15:16:40 +020042/* 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 */
45char quoted_str[NB_QSTR][QSTR_SIZE + 1];
46int quoted_idx = 0;
47
Willy Tarreaubaaee002006-06-26 02:48:02 +020048/*
William Lallemande7340ec2012-01-24 11:15:39 +010049 * unsigned long long ASCII representation
50 *
51 * return the last char '\0' or NULL if no enough
52 * space in dst
53 */
54char *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 */
157char *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 */
269char *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 */
291char *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 */
312char *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 Tarreaubaaee002006-06-26 02:48:02 +0200373 * 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 */
379int 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 Tarreau72d759c2007-10-25 12:14:10 +0200392 * This function simply returns a locally allocated string containing
Willy Tarreaubaaee002006-06-26 02:48:02 +0200393 * the ascii representation for number 'n' in decimal.
394 */
Emeric Brun3a7fce52010-01-04 14:54:38 +0100395char *ultoa_r(unsigned long n, char *buffer, int size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200396{
397 char *pos;
398
Willy Tarreau72d759c2007-10-25 12:14:10 +0200399 pos = buffer + size - 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200400 *pos-- = '\0';
401
402 do {
403 *pos-- = '0' + n % 10;
404 n /= 10;
Willy Tarreau72d759c2007-10-25 12:14:10 +0200405 } while (n && pos >= buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200406 return pos + 1;
407}
408
Willy Tarreau91092e52007-10-25 16:58:42 +0200409/*
Willy Tarreaue7239b52009-03-29 13:41:58 +0200410 * This function simply returns a locally allocated string containing
Thierry FOURNIER1480bd82015-06-06 19:14:59 +0200411 * the ascii representation for signed number 'n' in decimal.
412 */
413char *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 Tarreaue7239b52009-03-29 13:41:58 +0200427 * 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 */
431const 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 Tarreau91092e52007-10-25 16:58:42 +0200458 * 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 */
465const 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 Tarreau588297f2014-06-16 15:16:40 +0200470/* 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 */
476const 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 Tsai81ae1952007-12-05 10:47:29 +0100503/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200504 * 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 */
510int 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 Tarreau3ca1a882015-01-15 18:43:49 +0100524/* rounds <i> down to the closest value having max 2 digits */
525unsigned 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 Tarreau2e74c3f2007-12-02 18:45:09 +0100536/*
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 */
541const char *invalid_char(const char *name)
542{
543 if (!*name)
544 return name;
545
546 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100547 if (!isalnum((int)(unsigned char)*name) && *name != '.' && *name != ':' &&
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100548 *name != '_' && *name != '-')
549 return name;
550 name++;
551 }
552 return NULL;
553}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200554
555/*
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200556 * 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 */
560const char *invalid_domainchar(const char *name) {
561
562 if (!*name)
563 return name;
564
565 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100566 if (!isalnum((int)(unsigned char)*name) && *name != '.' &&
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200567 *name != '_' && *name != '-')
568 return name;
569
570 name++;
571 }
572
573 return NULL;
574}
575
576/*
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100577 * converts <str> to a struct sockaddr_storage* provided by the caller. The
Willy Tarreau24709282013-03-10 21:32:12 +0100578 * 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 Tarreaufab5a432011-03-04 15:31:53 +0100582 * 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 FOURNIER58639a02014-11-25 12:02:25 +0100587 * 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 Tarreaubaaee002006-06-26 02:48:02 +0200590 */
Thierry FOURNIER58639a02014-11-25 12:02:25 +0100591struct sockaddr_storage *str2ip2(const char *str, struct sockaddr_storage *sa, int resolve)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200592{
Willy Tarreaufab5a432011-03-04 15:31:53 +0100593 struct hostent *he;
594
Willy Tarreaufab5a432011-03-04 15:31:53 +0100595 /* Any IPv6 address */
596 if (str[0] == ':' && str[1] == ':' && !str[2]) {
Willy Tarreau24709282013-03-10 21:32:12 +0100597 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 Tarreauc120c8d2013-03-10 19:27:44 +0100601 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100602 }
603
Willy Tarreau24709282013-03-10 21:32:12 +0100604 /* Any address for the family, defaults to IPv4 */
Willy Tarreaufab5a432011-03-04 15:31:53 +0100605 if (!str[0] || (str[0] == '*' && !str[1])) {
Willy Tarreau24709282013-03-10 21:32:12 +0100606 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
607 sa->ss_family = AF_INET;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100608 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100609 }
610
611 /* check for IPv6 first */
Willy Tarreau24709282013-03-10 21:32:12 +0100612 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 Tarreauc120c8d2013-03-10 19:27:44 +0100614 sa->ss_family = AF_INET6;
615 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100616 }
617
618 /* then check for IPv4 */
Willy Tarreau24709282013-03-10 21:32:12 +0100619 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 Tarreauc120c8d2013-03-10 19:27:44 +0100621 sa->ss_family = AF_INET;
622 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100623 }
624
Thierry FOURNIER58639a02014-11-25 12:02:25 +0100625 if (!resolve)
626 return NULL;
627
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200628 if (!dns_hostname_validation(str, NULL))
629 return NULL;
630
David du Colombierd5f43282011-03-17 10:40:16 +0100631#ifdef USE_GETADDRINFO
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200632 if (global.tune.options & GTUNE_USE_GAI) {
David du Colombierd5f43282011-03-17 10:40:16 +0100633 struct addrinfo hints, *result;
634
635 memset(&result, 0, sizeof(result));
636 memset(&hints, 0, sizeof(hints));
Willy Tarreau24709282013-03-10 21:32:12 +0100637 hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC;
David du Colombierd5f43282011-03-17 10:40:16 +0100638 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 Tarreau24709282013-03-10 21:32:12 +0100643 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 Colombierd5f43282011-03-17 10:40:16 +0100648 switch (result->ai_family) {
649 case AF_INET:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100650 memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen);
651 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100652 case AF_INET6:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100653 memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen);
654 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100655 }
656 }
657
Sean Carey58ea0392013-02-15 23:39:18 +0100658 if (result)
659 freeaddrinfo(result);
Willy Tarreaufab5a432011-03-04 15:31:53 +0100660 }
David du Colombierd5f43282011-03-17 10:40:16 +0100661#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200662 /* 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 Colombierd5f43282011-03-17 10:40:16 +0100680 /* unsupported address family */
Willy Tarreau24709282013-03-10 21:32:12 +0100681 fail:
Willy Tarreaufab5a432011-03-04 15:31:53 +0100682 return NULL;
683}
684
685/*
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100686 * 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 Tarreau24709282013-03-10 21:32:12 +0100709 * 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 Tarreauccfccef2014-05-10 01:49:15 +0200714 * - '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 Tarreau40aa0702013-03-10 23:51:38 +0100719 * - "fd@" => an integer must follow, and is a file descriptor number.
Willy Tarreau24709282013-03-10 21:32:12 +0100720 *
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100721 * 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 Tarreau24709282013-03-10 21:32:12 +0100724 * always initialized if non-null, even for non-IP families.
Willy Tarreaud393a622013-03-04 18:22:00 +0100725 *
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 Tarreau40aa0702013-03-10 23:51:38 +0100728 *
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 Tarreaufab5a432011-03-04 15:31:53 +0100731 */
Willy Tarreaud393a622013-03-04 18:22:00 +0100732struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high, char **err, const char *pfx)
Willy Tarreaufab5a432011-03-04 15:31:53 +0100733{
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100734 static struct sockaddr_storage ss;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100735 struct sockaddr_storage *ret = NULL;
Willy Tarreau24709282013-03-10 21:32:12 +0100736 char *back, *str2;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100737 char *port1, *port2;
738 int portl, porth, porta;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200739 int abstract = 0;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100740
741 portl = porth = porta = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200742
Willy Tarreaudad36a32013-03-11 01:20:04 +0100743 str2 = back = env_expand(strdup(str));
Willy Tarreaudf350f12013-03-01 20:22:54 +0100744 if (str2 == NULL) {
745 memprintf(err, "out of memory in '%s'\n", __FUNCTION__);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100746 goto out;
Willy Tarreaudf350f12013-03-01 20:22:54 +0100747 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200748
Willy Tarreau24709282013-03-10 21:32:12 +0100749 memset(&ss, 0, sizeof(ss));
750
751 if (strncmp(str2, "unix@", 5) == 0) {
752 str2 += 5;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200753 abstract = 0;
Willy Tarreau24709282013-03-10 21:32:12 +0100754 ss.ss_family = AF_UNIX;
755 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200756 else if (strncmp(str2, "abns@", 5) == 0) {
757 str2 += 5;
758 abstract = 1;
759 ss.ss_family = AF_UNIX;
760 }
Willy Tarreau24709282013-03-10 21:32:12 +0100761 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 Tarreau40aa0702013-03-10 23:51:38 +0100775 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 Tarreaudad36a32013-03-11 01:20:04 +0100782 memprintf(err, "file descriptor '%s' is not a valid integer in '%s'\n", str2, str);
Willy Tarreau40aa0702013-03-10 23:51:38 +0100783 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 Tarreau15586382013-03-04 19:48:14 +0100790 int prefix_path_len;
791 int max_path_len;
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200792 int adr_len;
Willy Tarreau15586382013-03-04 19:48:14 +0100793
794 /* complete unix socket path name during startup or soft-restart is
795 * <unix_bind_prefix><path>.<pid>.<bak|tmp>
796 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200797 prefix_path_len = (pfx && !abstract) ? strlen(pfx) : 0;
Willy Tarreau15586382013-03-04 19:48:14 +0100798 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 Tarreau94ef3f32014-04-14 14:49:00 +0200801 adr_len = strlen(str2);
802 if (adr_len > max_path_len) {
Willy Tarreau15586382013-03-04 19:48:14 +0100803 memprintf(err, "socket path '%s' too long (max %d)\n", str, max_path_len);
804 goto out;
805 }
806
Willy Tarreauccfccef2014-05-10 01:49:15 +0200807 /* 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 Tarreau94ef3f32014-04-14 14:49:00 +0200809 if (prefix_path_len)
Willy Tarreau15586382013-03-04 19:48:14 +0100810 memcpy(((struct sockaddr_un *)&ss)->sun_path, pfx, prefix_path_len);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200811 memcpy(((struct sockaddr_un *)&ss)->sun_path + prefix_path_len + abstract, str2, adr_len + 1 - abstract);
Willy Tarreau15586382013-03-04 19:48:14 +0100812 }
Willy Tarreau24709282013-03-10 21:32:12 +0100813 else { /* IPv4 and IPv6 */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100814 port1 = strrchr(str2, ':');
815 if (port1)
816 *port1++ = '\0';
817 else
818 port1 = "";
Willy Tarreaubaaee002006-06-26 02:48:02 +0200819
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100820 if (str2ip(str2, &ss) == NULL) {
821 memprintf(err, "invalid address: '%s' in '%s'\n", str2, str);
822 goto out;
823 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100824
Willy Tarreaua39d1992013-04-01 20:37:42 +0200825 if (isdigit((int)(unsigned char)*port1)) { /* single port or range */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100826 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 Tarreaudad36a32013-03-11 01:20:04 +0100844 memprintf(err, "invalid character '%c' in port number '%s' in '%s'\n", *port1, port1, str);
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100845 goto out;
846 }
847 set_host_port(&ss, porta);
Willy Tarreaue4c58c82013-03-06 15:28:17 +0100848 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100849
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100850 ret = &ss;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100851 out:
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100852 if (low)
853 *low = portl;
854 if (high)
855 *high = porth;
Willy Tarreau24709282013-03-10 21:32:12 +0100856 free(back);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100857 return ret;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200858}
859
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100860/* 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 */
864int 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 FOURNIERb0504632013-12-14 15:39:02 +0100884/* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
885 * succeeds otherwise zero.
886 */
887int 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 Tarreauc6f4ce82009-06-10 11:09:37 +0200896/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200897 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200898 * 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 FOURNIERfc7ac7b2014-02-11 15:23:04 +0100902int str2net(const char *str, int resolve, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200903{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200904 __label__ out_free, out_err;
905 char *c, *s;
906 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200907
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200908 s = strdup(str);
909 if (!s)
910 return 0;
911
Willy Tarreaubaaee002006-06-26 02:48:02 +0200912 memset(mask, 0, sizeof(*mask));
913 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200914
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200915 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200916 *c++ = '\0';
917 /* c points to the mask */
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100918 if (!str2mask(c, mask))
919 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200920 }
921 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100922 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200923 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200924 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200925 struct hostent *he;
926
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100927 if (!resolve)
928 goto out_err;
929
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200930 if ((he = gethostbyname(s)) == NULL) {
931 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932 }
933 else
934 *addr = *(struct in_addr *) *(he->h_addr_list);
935 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200936
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 Tarreaubaaee002006-06-26 02:48:02 +0200944}
945
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100946
947/*
Willy Tarreau6d20e282012-04-27 22:49:47 +0200948 * 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 */
953int 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 Colombier6f5ccb12011-03-10 22:26:24 +0100989 * Parse IPv4 address found in url.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100990 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100991int url2ipv4(const char *addr, struct in_addr *dst)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100992{
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 FOURNIER9f95e402014-03-21 14:51:46 +01001032 * 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 Cassen5eb1a902007-11-29 15:43:32 +01001041 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001042int url2sa(const char *url, int ulen, struct sockaddr_storage *addr, struct split_url *out)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001043{
1044 const char *curr = url, *cp = url;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001045 const char *end;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001046 int ret, url_code = 0;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001047 unsigned long long int http_code = 0;
1048 int default_port;
1049 struct hostent *he;
1050 char *p;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001051
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 FOURNIER9f95e402014-03-21 14:51:46 +01001064 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 Cassen5eb1a902007-11-29 15:43:32 +01001071
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001072 /* 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 Cassen5eb1a902007-11-29 15:43:32 +01001154 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001155
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 Cassen5eb1a902007-11-29 15:43:32 +01001166 return -1;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001167
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 Cassen5eb1a902007-11-29 15:43:32 +01001194 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001195 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001196 return -1;
1197}
1198
Willy Tarreau631f01c2011-09-05 00:36:48 +02001199/* 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 */
1205int 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 Horman75ab8bd2014-06-16 09:39:41 +09001235/* 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 */
1241int 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 Tarreaubaaee002006-06-26 02:48:02 +02001269/* 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 */
1279const char hextab[16] = "0123456789ABCDEF";
1280char *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 FOURNIERe059ec92014-03-17 12:01:13 +01001303/*
1304 * Same behavior as encode_string() above, except that it encodes chunk
1305 * <chunk> instead of a string.
1306 */
1307char *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 FOURNIERddea6262015-05-28 16:00:28 +02001333/* 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 */
1358const 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 Tarreaubf9c2fc2011-05-31 18:06:18 +02001397/* Decode an URL-encoded string in-place. The resulting string might
1398 * be shorter. If some forbidden characters are found, the conversion is
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001399 * 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 Tarreaubf9c2fc2011-05-31 18:06:18 +02001401 */
1402int url_decode(char *string)
1403{
1404 char *in, *out;
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001405 int ret = -1;
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001406
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 FOURNIER5068d962013-10-04 16:27:27 +02001426 ret = out - string; /* success */
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001427 end:
1428 *out = 0;
1429 return ret;
1430}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001431
Willy Tarreau6911fa42007-03-04 18:06:08 +01001432unsigned int str2ui(const char *s)
1433{
1434 return __str2ui(s);
1435}
1436
1437unsigned int str2uic(const char *s)
1438{
1439 return __str2uic(s);
1440}
1441
1442unsigned int strl2ui(const char *s, int len)
1443{
1444 return __strl2ui(s, len);
1445}
1446
1447unsigned int strl2uic(const char *s, int len)
1448{
1449 return __strl2uic(s, len);
1450}
1451
Willy Tarreau4ec83cd2010-10-15 23:19:55 +02001452unsigned int read_uint(const char **s, const char *end)
1453{
1454 return __read_uint(s, end);
1455}
1456
Willy Tarreau6911fa42007-03-04 18:06:08 +01001457/* 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 */
1461int strl2ic(const char *s, int len)
1462{
1463 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001464 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001465
1466 if (len > 0) {
1467 if (*s != '-') {
1468 /* positive number */
1469 while (len-- > 0) {
1470 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001471 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001472 if (j > 9)
1473 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001474 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001475 }
1476 } else {
1477 /* negative number */
1478 s++;
1479 while (--len > 0) {
1480 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001481 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001482 if (j > 9)
1483 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001484 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001485 }
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 */
1500int 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 Tarreau6911fa42007-03-04 18:06:08 +01001543
1544int 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 FOURNIER511e9472014-01-23 17:40:34 +01001578/* 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 */
1588int 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 Tarreaua0d37b62007-12-02 22:00:35 +01001622/* 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 */
1629const 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 Tarreau6911fa42007-03-04 18:06:08 +01001700
Emeric Brun39132b22010-01-04 14:57:24 +01001701/* 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 */
1705const 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
Godbach58048a22015-01-28 17:36:16 +08001748 if (*text != '\0' && *++text != '\0')
1749 return text;
1750
Emeric Brun39132b22010-01-04 14:57:24 +01001751 *ret = value;
1752 return NULL;
1753}
1754
Willy Tarreau126d4062013-12-03 17:50:47 +01001755/*
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 FOURNIERee330af2014-01-21 11:36:14 +01001759 * error message in err. In succes case, it returns the consumed length.
Willy Tarreau126d4062013-12-03 17:50:47 +01001760 */
1761int 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 FOURNIER9645d422013-12-06 19:59:28 +01001766 int alloc;
Willy Tarreau126d4062013-12-03 17:50:47 +01001767
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 FOURNIER9645d422013-12-06 19:59:28 +01001775
Willy Tarreau126d4062013-12-03 17:50:47 +01001776 if (!*binstr) {
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001777 *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 Tarreau126d4062013-12-03 17:50:47 +01001783 }
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001784 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 Tarreau126d4062013-12-03 17:50:47 +01001793
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 FOURNIERee330af2014-01-21 11:36:14 +01001802 return len << 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001803
1804bad_input:
1805 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001806 if (alloc)
1807 free(binstr);
Willy Tarreau126d4062013-12-03 17:50:47 +01001808 return 0;
1809}
1810
Willy Tarreau946ba592009-05-10 15:41:18 +02001811/* copies at most <n> characters from <src> and always terminates with '\0' */
1812char *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 Assmannbb77c8e2013-10-06 23:24:13 +02001828/*
1829 * search needle in haystack
1830 * returns the pointer if found, returns NULL otherwise
1831 */
1832const 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 Tarreau482b00d2009-10-04 22:48:42 +02001853/* 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 */
1856unsigned 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 Tarreau348238b2010-01-18 15:05:57 +01001869/* 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 */
1875int 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 Tarreau482b00d2009-10-04 22:48:42 +02001898
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001899/* 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 */
1904unsigned 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 */
1954unsigned 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 Tarreau74172752010-10-15 23:21:42 +02002005unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret)
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02002006{
2007 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
2008 register unsigned int dig100, dig10, dig1;
2009 int s;
Willy Tarreau74172752010-10-15 23:21:42 +02002010 char *p, *d;
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02002011
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 Tarreauf0b38bf2010-06-06 13:22:23 +02002053/* 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 Tarreau075415a2013-12-12 11:29:39 +01002057 * The destination is only modified on success.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02002058 */
2059int 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 FOURNIERd559dd82013-11-22 16:16:59 +01002106/* 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 Tarreau075415a2013-12-12 11:29:39 +01002109 * The destination is only modified on success.
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002110 */
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002111int buf2ip6(const char *buf, size_t len, struct in6_addr *dst)
2112{
Thierry FOURNIERcd659912013-12-11 12:33:54 +01002113 char null_term_ip6[INET6_ADDRSTRLEN + 1];
Willy Tarreau075415a2013-12-12 11:29:39 +01002114 struct in6_addr out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002115
Thierry FOURNIERcd659912013-12-11 12:33:54 +01002116 if (len > INET6_ADDRSTRLEN)
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002117 return 0;
2118
2119 memcpy(null_term_ip6, buf, len);
2120 null_term_ip6[len] = '\0';
2121
Willy Tarreau075415a2013-12-12 11:29:39 +01002122 if (!inet_pton(AF_INET6, null_term_ip6, &out))
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002123 return 0;
2124
Willy Tarreau075415a2013-12-12 11:29:39 +01002125 *dst = out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002126 return 1;
2127}
2128
Willy Tarreauacf95772010-06-14 19:09:21 +02002129/* 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 */
2133const 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 Tarreaude2dd6b2013-01-24 02:14:42 +01002141 for (i = 1; i < sizeof(val) - 2 && *ptr; i++)
Willy Tarreauacf95772010-06-14 19:09:21 +02002142 val[i] = *ptr++;
2143 val[i++] = '\'';
2144 val[i] = '\0';
2145 return val;
2146}
2147
Willy Tarreau5b180202010-07-18 10:40:48 +02002148/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
2149int 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 Tarreau4c14eaa2010-11-24 14:01:45 +01002171/* hash a 32-bit integer to another 32-bit integer */
2172unsigned int full_hash(unsigned int a)
2173{
2174 return __full_hash(a);
2175}
2176
David du Colombier4f92d322011-03-24 11:09:31 +01002177/* Return non-zero if IPv4 address is part of the network,
2178 * otherwise zero.
2179 */
2180int 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 */
2188int 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 */
2200const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00,
2201 0x00, 0x00, 0x00, 0x00,
2202 0x00, 0x00, 0xFF, 0xFF };
2203
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002204/* Map IPv4 adress on IPv6 address, as specified in RFC 3513.
2205 * Input and output may overlap.
2206 */
David du Colombier4f92d322011-03-24 11:09:31 +01002207void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr)
2208{
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002209 struct in_addr tmp_addr;
2210
2211 tmp_addr.s_addr = sin_addr->s_addr;
David du Colombier4f92d322011-03-24 11:09:31 +01002212 memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx));
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002213 memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4);
David du Colombier4f92d322011-03-24 11:09:31 +01002214}
2215
2216/* Map IPv6 adress on IPv4 address, as specified in RFC 3513.
2217 * Return true if conversion is possible and false otherwise.
2218 */
2219int 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 Lallemand421f5b52012-02-06 18:15:57 +01002230char *human_time(int t, short hz_div) {
2231 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
2232 char *p = rv;
Willy Tarreau761b3d52014-04-14 14:53:06 +02002233 char *end = rv + sizeof(rv);
William Lallemand421f5b52012-02-06 18:15:57 +01002234 int cnt=2; // print two numbers
2235
2236 if (unlikely(t < 0 || hz_div <= 0)) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002237 snprintf(p, end - p, "?");
William Lallemand421f5b52012-02-06 18:15:57 +01002238 return rv;
2239 }
2240
2241 if (unlikely(hz_div > 1))
2242 t /= hz_div;
2243
2244 if (t >= DAY) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002245 p += snprintf(p, end - p, "%dd", t / DAY);
William Lallemand421f5b52012-02-06 18:15:57 +01002246 cnt--;
2247 }
2248
2249 if (cnt && t % DAY / HOUR) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002250 p += snprintf(p, end - p, "%dh", t % DAY / HOUR);
William Lallemand421f5b52012-02-06 18:15:57 +01002251 cnt--;
2252 }
2253
2254 if (cnt && t % HOUR / MINUTE) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002255 p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE);
William Lallemand421f5b52012-02-06 18:15:57 +01002256 cnt--;
2257 }
2258
2259 if ((cnt && t % MINUTE) || !t) // also display '0s'
Willy Tarreau761b3d52014-04-14 14:53:06 +02002260 p += snprintf(p, end - p, "%ds", t % MINUTE / SEC);
William Lallemand421f5b52012-02-06 18:15:57 +01002261
2262 return rv;
2263}
2264
2265const 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 */
2278char *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 */
2309char *gmt2str_log(char *dst, struct tm *tm, size_t size)
2310{
Yuxans Yao4e25b012012-10-19 10:36:09 +08002311 if (size < 27) /* the size is fixed: 26 chars + \0 */
William Lallemand421f5b52012-02-06 18:15:57 +01002312 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 Yao4e25b012012-10-19 10:36:09 +08002337/* 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 */
2342char *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 Tarreau9a7bea52012-04-27 11:16:50 +02002367/* 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 Tarreaueb6cead2012-09-20 19:43:14 +02002384 * 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 Tarreau9a7bea52012-04-27 11:16:50 +02002386 *
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 */
2394char *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 Tarreaueb6cead2012-09-20 19:43:14 +02002401 if (!out)
2402 return NULL;
2403
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002404 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 Tarreau1b2fed62013-04-01 22:48:54 +02002410 needed = vsnprintf(ret, allocated, format, args);
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002411 va_end(args);
2412
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002413 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 Tarreau9a7bea52012-04-27 11:16:50 +02002423
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002424 allocated = needed + 1;
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002425 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 Lallemand421f5b52012-02-06 18:15:57 +01002441
Willy Tarreau21c705b2012-09-14 11:40:36 +02002442/* 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 Tarreau70eec382012-10-10 08:56:47 +02002444 * freed by the caller. It also supports being passed a NULL which results in the same
2445 * output.
Willy Tarreau21c705b2012-09-14 11:40:36 +02002446 * Example of use :
2447 * parse(cmd, &err); (callee: memprintf(&err, ...))
2448 * fprintf(stderr, "Parser said: %s\n", indent_error(&err));
2449 * free(err);
2450 */
2451char *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 Tarreau70eec382012-10-10 08:56:47 +02002459 if (!out || !*out)
2460 return NULL;
2461
Willy Tarreau21c705b2012-09-14 11:40:36 +02002462 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 Tarreaudad36a32013-03-11 01:20:04 +01002514/* 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 */
2521char *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 Guillaume88c278f2013-04-15 19:27:10 +02002591
2592/* same as strstr() but case-insensitive and with limit length */
2593const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2)
2594{
2595 char *pptr, *sptr, *start;
Willy Tarreauc8746532014-05-28 23:05:07 +02002596 unsigned int slen, plen;
2597 unsigned int tmp1, tmp2;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002598
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 FOURNIER317e1c42014-08-12 10:20:47 +02002640/* 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 */
2646unsigned 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 FOURNIER9e7ec082015-03-12 19:32:38 +01002746 if (( *c <= 0x7f && (p-(unsigned char *)s) > 1) ||
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002747 (*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 Tarreaubaaee002006-06-26 02:48:02 +02002760/*
2761 * Local variables:
2762 * c-indent-level: 8
2763 * c-basic-offset: 8
2764 * End:
2765 */