blob: 464173937c64164c78318d8e18084f34dfed956e [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 FOURNIER763a5d82015-07-06 23:09:52 +0200411 * the ascii representation for number 'n' in decimal.
412 */
413char *lltoa_r(long long int in, char *buffer, int size)
414{
415 char *pos;
416 int neg = 0;
417 unsigned long long int n;
418
419 pos = buffer + size - 1;
420 *pos-- = '\0';
421
422 if (in < 0) {
423 neg = 1;
424 n = -in;
425 }
426 else
427 n = in;
428
429 do {
430 *pos-- = '0' + n % 10;
431 n /= 10;
432 } while (n && pos >= buffer);
433 if (neg && pos > buffer)
434 *pos-- = '-';
435 return pos + 1;
436}
437
438/*
439 * This function simply returns a locally allocated string containing
Thierry FOURNIER1480bd82015-06-06 19:14:59 +0200440 * the ascii representation for signed number 'n' in decimal.
441 */
442char *sltoa_r(long n, char *buffer, int size)
443{
444 char *pos;
445
446 if (n >= 0)
447 return ultoa_r(n, buffer, size);
448
449 pos = ultoa_r(-n, buffer + 1, size - 1) - 1;
450 *pos = '-';
451 return pos;
452}
453
454/*
455 * This function simply returns a locally allocated string containing
Willy Tarreaue7239b52009-03-29 13:41:58 +0200456 * the ascii representation for number 'n' in decimal, formatted for
457 * HTML output with tags to create visual grouping by 3 digits. The
458 * output needs to support at least 171 characters.
459 */
460const char *ulltoh_r(unsigned long long n, char *buffer, int size)
461{
462 char *start;
463 int digit = 0;
464
465 start = buffer + size;
466 *--start = '\0';
467
468 do {
469 if (digit == 3 && start >= buffer + 7)
470 memcpy(start -= 7, "</span>", 7);
471
472 if (start >= buffer + 1) {
473 *--start = '0' + n % 10;
474 n /= 10;
475 }
476
477 if (digit == 3 && start >= buffer + 18)
478 memcpy(start -= 18, "<span class=\"rls\">", 18);
479
480 if (digit++ == 3)
481 digit = 1;
482 } while (n && start > buffer);
483 return start;
484}
485
486/*
Willy Tarreau91092e52007-10-25 16:58:42 +0200487 * This function simply returns a locally allocated string containing the ascii
488 * representation for number 'n' in decimal, unless n is 0 in which case it
489 * returns the alternate string (or an empty string if the alternate string is
490 * NULL). It use is intended for limits reported in reports, where it's
491 * desirable not to display anything if there is no limit. Warning! it shares
492 * the same vector as ultoa_r().
493 */
494const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
495{
496 return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
497}
498
Willy Tarreau588297f2014-06-16 15:16:40 +0200499/* returns a locally allocated string containing the quoted encoding of the
500 * input string. The output may be truncated to QSTR_SIZE chars, but it is
501 * guaranteed that the string will always be properly terminated. Quotes are
502 * encoded by doubling them as is commonly done in CSV files. QSTR_SIZE must
503 * always be at least 4 chars.
504 */
505const char *qstr(const char *str)
506{
507 char *ret = quoted_str[quoted_idx];
508 char *p, *end;
509
510 if (++quoted_idx >= NB_QSTR)
511 quoted_idx = 0;
512
513 p = ret;
514 end = ret + QSTR_SIZE;
515
516 *p++ = '"';
517
518 /* always keep 3 chars to support passing "" and the ending " */
519 while (*str && p < end - 3) {
520 if (*str == '"') {
521 *p++ = '"';
522 *p++ = '"';
523 }
524 else
525 *p++ = *str;
526 str++;
527 }
528 *p++ = '"';
529 return ret;
530}
531
Robert Tsai81ae1952007-12-05 10:47:29 +0100532/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200533 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
534 *
535 * It looks like this one would be a good candidate for inlining, but this is
536 * not interesting because it around 35 bytes long and often called multiple
537 * times within the same function.
538 */
539int ishex(char s)
540{
541 s -= '0';
542 if ((unsigned char)s <= 9)
543 return 1;
544 s -= 'A' - '0';
545 if ((unsigned char)s <= 5)
546 return 1;
547 s -= 'a' - 'A';
548 if ((unsigned char)s <= 5)
549 return 1;
550 return 0;
551}
552
Willy Tarreau3ca1a882015-01-15 18:43:49 +0100553/* rounds <i> down to the closest value having max 2 digits */
554unsigned int round_2dig(unsigned int i)
555{
556 unsigned int mul = 1;
557
558 while (i >= 100) {
559 i /= 10;
560 mul *= 10;
561 }
562 return i * mul;
563}
564
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100565/*
566 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
567 * invalid character is found, a pointer to it is returned. If everything is
568 * fine, NULL is returned.
569 */
570const char *invalid_char(const char *name)
571{
572 if (!*name)
573 return name;
574
575 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100576 if (!isalnum((int)(unsigned char)*name) && *name != '.' && *name != ':' &&
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100577 *name != '_' && *name != '-')
578 return name;
579 name++;
580 }
581 return NULL;
582}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200583
584/*
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200585 * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-].
586 * If an invalid character is found, a pointer to it is returned.
587 * If everything is fine, NULL is returned.
588 */
589const char *invalid_domainchar(const char *name) {
590
591 if (!*name)
592 return name;
593
594 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100595 if (!isalnum((int)(unsigned char)*name) && *name != '.' &&
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200596 *name != '_' && *name != '-')
597 return name;
598
599 name++;
600 }
601
602 return NULL;
603}
604
605/*
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100606 * converts <str> to a struct sockaddr_storage* provided by the caller. The
Willy Tarreau24709282013-03-10 21:32:12 +0100607 * caller must have zeroed <sa> first, and may have set sa->ss_family to force
608 * parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then
609 * the function tries to guess the address family from the syntax. If the
610 * family is forced and the format doesn't match, an error is returned. The
Willy Tarreaufab5a432011-03-04 15:31:53 +0100611 * string is assumed to contain only an address, no port. The address can be a
612 * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to
613 * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved.
614 * The return address will only have the address family and the address set,
615 * all other fields remain zero. The string is not supposed to be modified.
Thierry FOURNIER58639a02014-11-25 12:02:25 +0100616 * The IPv6 '::' address is IN6ADDR_ANY. If <resolve> is non-zero, the hostname
617 * is resolved, otherwise only IP addresses are resolved, and anything else
618 * returns NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200619 */
Thierry FOURNIER58639a02014-11-25 12:02:25 +0100620struct sockaddr_storage *str2ip2(const char *str, struct sockaddr_storage *sa, int resolve)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200621{
Willy Tarreaufab5a432011-03-04 15:31:53 +0100622 struct hostent *he;
623
Willy Tarreaufab5a432011-03-04 15:31:53 +0100624 /* Any IPv6 address */
625 if (str[0] == ':' && str[1] == ':' && !str[2]) {
Willy Tarreau24709282013-03-10 21:32:12 +0100626 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
627 sa->ss_family = AF_INET6;
628 else if (sa->ss_family != AF_INET6)
629 goto fail;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100630 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100631 }
632
Willy Tarreau24709282013-03-10 21:32:12 +0100633 /* Any address for the family, defaults to IPv4 */
Willy Tarreaufab5a432011-03-04 15:31:53 +0100634 if (!str[0] || (str[0] == '*' && !str[1])) {
Willy Tarreau24709282013-03-10 21:32:12 +0100635 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
636 sa->ss_family = AF_INET;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100637 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100638 }
639
640 /* check for IPv6 first */
Willy Tarreau24709282013-03-10 21:32:12 +0100641 if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET6) &&
642 inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)sa)->sin6_addr)) {
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100643 sa->ss_family = AF_INET6;
644 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100645 }
646
647 /* then check for IPv4 */
Willy Tarreau24709282013-03-10 21:32:12 +0100648 if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET) &&
649 inet_pton(AF_INET, str, &((struct sockaddr_in *)sa)->sin_addr)) {
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100650 sa->ss_family = AF_INET;
651 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100652 }
653
Thierry FOURNIER58639a02014-11-25 12:02:25 +0100654 if (!resolve)
655 return NULL;
656
Baptiste Assmanna68ca962015-04-14 01:15:08 +0200657 if (!dns_hostname_validation(str, NULL))
658 return NULL;
659
David du Colombierd5f43282011-03-17 10:40:16 +0100660#ifdef USE_GETADDRINFO
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200661 if (global.tune.options & GTUNE_USE_GAI) {
David du Colombierd5f43282011-03-17 10:40:16 +0100662 struct addrinfo hints, *result;
663
664 memset(&result, 0, sizeof(result));
665 memset(&hints, 0, sizeof(hints));
Willy Tarreau24709282013-03-10 21:32:12 +0100666 hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC;
David du Colombierd5f43282011-03-17 10:40:16 +0100667 hints.ai_socktype = SOCK_DGRAM;
668 hints.ai_flags = AI_PASSIVE;
669 hints.ai_protocol = 0;
670
671 if (getaddrinfo(str, NULL, &hints, &result) == 0) {
Willy Tarreau24709282013-03-10 21:32:12 +0100672 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
673 sa->ss_family = result->ai_family;
674 else if (sa->ss_family != result->ai_family)
675 goto fail;
676
David du Colombierd5f43282011-03-17 10:40:16 +0100677 switch (result->ai_family) {
678 case AF_INET:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100679 memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen);
680 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100681 case AF_INET6:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100682 memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen);
683 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100684 }
685 }
686
Sean Carey58ea0392013-02-15 23:39:18 +0100687 if (result)
688 freeaddrinfo(result);
Willy Tarreaufab5a432011-03-04 15:31:53 +0100689 }
David du Colombierd5f43282011-03-17 10:40:16 +0100690#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200691 /* try to resolve an IPv4/IPv6 hostname */
692 he = gethostbyname(str);
693 if (he) {
694 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
695 sa->ss_family = he->h_addrtype;
696 else if (sa->ss_family != he->h_addrtype)
697 goto fail;
698
699 switch (sa->ss_family) {
700 case AF_INET:
701 ((struct sockaddr_in *)sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
702 return sa;
703 case AF_INET6:
704 ((struct sockaddr_in6 *)sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
705 return sa;
706 }
707 }
708
David du Colombierd5f43282011-03-17 10:40:16 +0100709 /* unsupported address family */
Willy Tarreau24709282013-03-10 21:32:12 +0100710 fail:
Willy Tarreaufab5a432011-03-04 15:31:53 +0100711 return NULL;
712}
713
714/*
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100715 * Converts <str> to a locally allocated struct sockaddr_storage *, and a port
716 * range or offset consisting in two integers that the caller will have to
717 * check to find the relevant input format. The following format are supported :
718 *
719 * String format | address | port | low | high
720 * addr | <addr> | 0 | 0 | 0
721 * addr: | <addr> | 0 | 0 | 0
722 * addr:port | <addr> | <port> | <port> | <port>
723 * addr:pl-ph | <addr> | <pl> | <pl> | <ph>
724 * addr:+port | <addr> | <port> | 0 | <port>
725 * addr:-port | <addr> |-<port> | <port> | 0
726 *
727 * The detection of a port range or increment by the caller is made by
728 * comparing <low> and <high>. If both are equal, then port 0 means no port
729 * was specified. The caller may pass NULL for <low> and <high> if it is not
730 * interested in retrieving port ranges.
731 *
732 * Note that <addr> above may also be :
733 * - empty ("") => family will be AF_INET and address will be INADDR_ANY
734 * - "*" => family will be AF_INET and address will be INADDR_ANY
735 * - "::" => family will be AF_INET6 and address will be IN6ADDR_ANY
736 * - a host name => family and address will depend on host name resolving.
737 *
Willy Tarreau24709282013-03-10 21:32:12 +0100738 * A prefix may be passed in before the address above to force the family :
739 * - "ipv4@" => force address to resolve as IPv4 and fail if not possible.
740 * - "ipv6@" => force address to resolve as IPv6 and fail if not possible.
741 * - "unix@" => force address to be a path to a UNIX socket even if the
742 * path does not start with a '/'
Willy Tarreauccfccef2014-05-10 01:49:15 +0200743 * - 'abns@' -> force address to belong to the abstract namespace (Linux
744 * only). These sockets are just like Unix sockets but without
745 * the need for an underlying file system. The address is a
746 * string. Technically it's like a Unix socket with a zero in
747 * the first byte of the address.
Willy Tarreau40aa0702013-03-10 23:51:38 +0100748 * - "fd@" => an integer must follow, and is a file descriptor number.
Willy Tarreau24709282013-03-10 21:32:12 +0100749 *
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100750 * Also note that in order to avoid any ambiguity with IPv6 addresses, the ':'
751 * is mandatory after the IP address even when no port is specified. NULL is
752 * returned if the address cannot be parsed. The <low> and <high> ports are
Willy Tarreau24709282013-03-10 21:32:12 +0100753 * always initialized if non-null, even for non-IP families.
Willy Tarreaud393a622013-03-04 18:22:00 +0100754 *
755 * If <pfx> is non-null, it is used as a string prefix before any path-based
756 * address (typically the path to a unix socket).
Willy Tarreau40aa0702013-03-10 23:51:38 +0100757 *
758 * When a file descriptor is passed, its value is put into the s_addr part of
759 * the address when cast to sockaddr_in and the address family is AF_UNSPEC.
Willy Tarreaufab5a432011-03-04 15:31:53 +0100760 */
Willy Tarreaud393a622013-03-04 18:22:00 +0100761struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high, char **err, const char *pfx)
Willy Tarreaufab5a432011-03-04 15:31:53 +0100762{
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100763 static struct sockaddr_storage ss;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100764 struct sockaddr_storage *ret = NULL;
Willy Tarreau24709282013-03-10 21:32:12 +0100765 char *back, *str2;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100766 char *port1, *port2;
767 int portl, porth, porta;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200768 int abstract = 0;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100769
770 portl = porth = porta = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200771
Willy Tarreaudad36a32013-03-11 01:20:04 +0100772 str2 = back = env_expand(strdup(str));
Willy Tarreaudf350f12013-03-01 20:22:54 +0100773 if (str2 == NULL) {
774 memprintf(err, "out of memory in '%s'\n", __FUNCTION__);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100775 goto out;
Willy Tarreaudf350f12013-03-01 20:22:54 +0100776 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200777
Willy Tarreau24709282013-03-10 21:32:12 +0100778 memset(&ss, 0, sizeof(ss));
779
780 if (strncmp(str2, "unix@", 5) == 0) {
781 str2 += 5;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200782 abstract = 0;
Willy Tarreau24709282013-03-10 21:32:12 +0100783 ss.ss_family = AF_UNIX;
784 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200785 else if (strncmp(str2, "abns@", 5) == 0) {
786 str2 += 5;
787 abstract = 1;
788 ss.ss_family = AF_UNIX;
789 }
Willy Tarreau24709282013-03-10 21:32:12 +0100790 else if (strncmp(str2, "ipv4@", 5) == 0) {
791 str2 += 5;
792 ss.ss_family = AF_INET;
793 }
794 else if (strncmp(str2, "ipv6@", 5) == 0) {
795 str2 += 5;
796 ss.ss_family = AF_INET6;
797 }
798 else if (*str2 == '/') {
799 ss.ss_family = AF_UNIX;
800 }
801 else
802 ss.ss_family = AF_UNSPEC;
803
Willy Tarreau40aa0702013-03-10 23:51:38 +0100804 if (ss.ss_family == AF_UNSPEC && strncmp(str2, "fd@", 3) == 0) {
805 char *endptr;
806
807 str2 += 3;
808 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = strtol(str2, &endptr, 10);
809
810 if (!*str2 || *endptr) {
Willy Tarreaudad36a32013-03-11 01:20:04 +0100811 memprintf(err, "file descriptor '%s' is not a valid integer in '%s'\n", str2, str);
Willy Tarreau40aa0702013-03-10 23:51:38 +0100812 goto out;
813 }
814
815 /* we return AF_UNSPEC if we use a file descriptor number */
816 ss.ss_family = AF_UNSPEC;
817 }
818 else if (ss.ss_family == AF_UNIX) {
Willy Tarreau15586382013-03-04 19:48:14 +0100819 int prefix_path_len;
820 int max_path_len;
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200821 int adr_len;
Willy Tarreau15586382013-03-04 19:48:14 +0100822
823 /* complete unix socket path name during startup or soft-restart is
824 * <unix_bind_prefix><path>.<pid>.<bak|tmp>
825 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200826 prefix_path_len = (pfx && !abstract) ? strlen(pfx) : 0;
Willy Tarreau15586382013-03-04 19:48:14 +0100827 max_path_len = (sizeof(((struct sockaddr_un *)&ss)->sun_path) - 1) -
828 (prefix_path_len ? prefix_path_len + 1 + 5 + 1 + 3 : 0);
829
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200830 adr_len = strlen(str2);
831 if (adr_len > max_path_len) {
Willy Tarreau15586382013-03-04 19:48:14 +0100832 memprintf(err, "socket path '%s' too long (max %d)\n", str, max_path_len);
833 goto out;
834 }
835
Willy Tarreauccfccef2014-05-10 01:49:15 +0200836 /* when abstract==1, we skip the first zero and copy all bytes except the trailing zero */
837 memset(((struct sockaddr_un *)&ss)->sun_path, 0, sizeof(((struct sockaddr_un *)&ss)->sun_path));
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200838 if (prefix_path_len)
Willy Tarreau15586382013-03-04 19:48:14 +0100839 memcpy(((struct sockaddr_un *)&ss)->sun_path, pfx, prefix_path_len);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200840 memcpy(((struct sockaddr_un *)&ss)->sun_path + prefix_path_len + abstract, str2, adr_len + 1 - abstract);
Willy Tarreau15586382013-03-04 19:48:14 +0100841 }
Willy Tarreau24709282013-03-10 21:32:12 +0100842 else { /* IPv4 and IPv6 */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100843 port1 = strrchr(str2, ':');
844 if (port1)
845 *port1++ = '\0';
846 else
847 port1 = "";
Willy Tarreaubaaee002006-06-26 02:48:02 +0200848
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100849 if (str2ip(str2, &ss) == NULL) {
850 memprintf(err, "invalid address: '%s' in '%s'\n", str2, str);
851 goto out;
852 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100853
Willy Tarreaua39d1992013-04-01 20:37:42 +0200854 if (isdigit((int)(unsigned char)*port1)) { /* single port or range */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100855 port2 = strchr(port1, '-');
856 if (port2)
857 *port2++ = '\0';
858 else
859 port2 = port1;
860 portl = atoi(port1);
861 porth = atoi(port2);
862 porta = portl;
863 }
864 else if (*port1 == '-') { /* negative offset */
865 portl = atoi(port1 + 1);
866 porta = -portl;
867 }
868 else if (*port1 == '+') { /* positive offset */
869 porth = atoi(port1 + 1);
870 porta = porth;
871 }
872 else if (*port1) { /* other any unexpected char */
Willy Tarreaudad36a32013-03-11 01:20:04 +0100873 memprintf(err, "invalid character '%c' in port number '%s' in '%s'\n", *port1, port1, str);
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100874 goto out;
875 }
876 set_host_port(&ss, porta);
Willy Tarreaue4c58c82013-03-06 15:28:17 +0100877 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100878
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100879 ret = &ss;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100880 out:
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100881 if (low)
882 *low = portl;
883 if (high)
884 *high = porth;
Willy Tarreau24709282013-03-10 21:32:12 +0100885 free(back);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100886 return ret;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200887}
888
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100889/* converts <str> to a struct in_addr containing a network mask. It can be
890 * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1
891 * if the conversion succeeds otherwise non-zero.
892 */
893int str2mask(const char *str, struct in_addr *mask)
894{
895 if (strchr(str, '.') != NULL) { /* dotted notation */
896 if (!inet_pton(AF_INET, str, mask))
897 return 0;
898 }
899 else { /* mask length */
900 char *err;
901 unsigned long len = strtol(str, &err, 10);
902
903 if (!*str || (err && *err) || (unsigned)len > 32)
904 return 0;
905 if (len)
906 mask->s_addr = htonl(~0UL << (32 - len));
907 else
908 mask->s_addr = 0;
909 }
910 return 1;
911}
912
Thierry FOURNIERb0504632013-12-14 15:39:02 +0100913/* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
914 * succeeds otherwise zero.
915 */
916int cidr2dotted(int cidr, struct in_addr *mask) {
917
918 if (cidr < 0 || cidr > 32)
919 return 0;
920
921 mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0;
922 return 1;
923}
924
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200925/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200926 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200927 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
928 * is optionnal and either in the dotted or CIDR notation.
929 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
930 */
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100931int str2net(const char *str, int resolve, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200932{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200933 __label__ out_free, out_err;
934 char *c, *s;
935 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200936
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200937 s = strdup(str);
938 if (!s)
939 return 0;
940
Willy Tarreaubaaee002006-06-26 02:48:02 +0200941 memset(mask, 0, sizeof(*mask));
942 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200943
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200944 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200945 *c++ = '\0';
946 /* c points to the mask */
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100947 if (!str2mask(c, mask))
948 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200949 }
950 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100951 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200952 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200953 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200954 struct hostent *he;
955
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100956 if (!resolve)
957 goto out_err;
958
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200959 if ((he = gethostbyname(s)) == NULL) {
960 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200961 }
962 else
963 *addr = *(struct in_addr *) *(he->h_addr_list);
964 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200965
966 ret_val = 1;
967 out_free:
968 free(s);
969 return ret_val;
970 out_err:
971 ret_val = 0;
972 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200973}
974
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100975
976/*
Willy Tarreau6d20e282012-04-27 22:49:47 +0200977 * converts <str> to two struct in6_addr* which must be pre-allocated.
978 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
979 * is an optionnal number of bits (128 being the default).
980 * Returns 1 if OK, 0 if error.
981 */
982int str62net(const char *str, struct in6_addr *addr, unsigned char *mask)
983{
984 char *c, *s;
985 int ret_val = 0;
986 char *err;
987 unsigned long len = 128;
988
989 s = strdup(str);
990 if (!s)
991 return 0;
992
993 memset(mask, 0, sizeof(*mask));
994 memset(addr, 0, sizeof(*addr));
995
996 if ((c = strrchr(s, '/')) != NULL) {
997 *c++ = '\0'; /* c points to the mask */
998 if (!*c)
999 goto out_free;
1000
1001 len = strtoul(c, &err, 10);
1002 if ((err && *err) || (unsigned)len > 128)
1003 goto out_free;
1004 }
1005 *mask = len; /* OK we have a valid mask in <len> */
1006
1007 if (!inet_pton(AF_INET6, s, addr))
1008 goto out_free;
1009
1010 ret_val = 1;
1011 out_free:
1012 free(s);
1013 return ret_val;
1014}
1015
1016
1017/*
David du Colombier6f5ccb12011-03-10 22:26:24 +01001018 * Parse IPv4 address found in url.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001019 */
David du Colombier6f5ccb12011-03-10 22:26:24 +01001020int url2ipv4(const char *addr, struct in_addr *dst)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001021{
1022 int saw_digit, octets, ch;
1023 u_char tmp[4], *tp;
1024 const char *cp = addr;
1025
1026 saw_digit = 0;
1027 octets = 0;
1028 *(tp = tmp) = 0;
1029
1030 while (*addr) {
1031 unsigned char digit = (ch = *addr++) - '0';
1032 if (digit > 9 && ch != '.')
1033 break;
1034 if (digit <= 9) {
1035 u_int new = *tp * 10 + digit;
1036 if (new > 255)
1037 return 0;
1038 *tp = new;
1039 if (!saw_digit) {
1040 if (++octets > 4)
1041 return 0;
1042 saw_digit = 1;
1043 }
1044 } else if (ch == '.' && saw_digit) {
1045 if (octets == 4)
1046 return 0;
1047 *++tp = 0;
1048 saw_digit = 0;
1049 } else
1050 return 0;
1051 }
1052
1053 if (octets < 4)
1054 return 0;
1055
1056 memcpy(&dst->s_addr, tmp, 4);
1057 return addr-cp-1;
1058}
1059
1060/*
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001061 * Resolve destination server from URL. Convert <str> to a sockaddr_storage.
1062 * <out> contain the code of the dectected scheme, the start and length of
1063 * the hostname. Actually only http and https are supported. <out> can be NULL.
1064 * This function returns the consumed length. It is useful if you parse complete
1065 * url like http://host:port/path, because the consumed length corresponds to
1066 * the first character of the path. If the conversion fails, it returns -1.
1067 *
1068 * This function tries to resolve the DNS name if haproxy is in starting mode.
1069 * So, this function may be used during the configuration parsing.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001070 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001071int url2sa(const char *url, int ulen, struct sockaddr_storage *addr, struct split_url *out)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001072{
1073 const char *curr = url, *cp = url;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001074 const char *end;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001075 int ret, url_code = 0;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001076 unsigned long long int http_code = 0;
1077 int default_port;
1078 struct hostent *he;
1079 char *p;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001080
1081 /* Firstly, try to find :// pattern */
1082 while (curr < url+ulen && url_code != 0x3a2f2f) {
1083 url_code = ((url_code & 0xffff) << 8);
1084 url_code += (unsigned char)*curr++;
1085 }
1086
1087 /* Secondly, if :// pattern is found, verify parsed stuff
1088 * before pattern is matching our http pattern.
1089 * If so parse ip address and port in uri.
1090 *
1091 * WARNING: Current code doesn't support dynamic async dns resolver.
1092 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001093 if (url_code != 0x3a2f2f)
1094 return -1;
1095
1096 /* Copy scheme, and utrn to lower case. */
1097 while (cp < curr - 3)
1098 http_code = (http_code << 8) + *cp++;
1099 http_code |= 0x2020202020202020ULL; /* Turn everything to lower case */
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001100
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001101 /* HTTP or HTTPS url matching */
1102 if (http_code == 0x2020202068747470ULL) {
1103 default_port = 80;
1104 if (out)
1105 out->scheme = SCH_HTTP;
1106 }
1107 else if (http_code == 0x2020206874747073ULL) {
1108 default_port = 443;
1109 if (out)
1110 out->scheme = SCH_HTTPS;
1111 }
1112 else
1113 return -1;
1114
1115 /* If the next char is '[', the host address is IPv6. */
1116 if (*curr == '[') {
1117 curr++;
1118
1119 /* Check trash size */
1120 if (trash.size < ulen)
1121 return -1;
1122
1123 /* Look for ']' and copy the address in a trash buffer. */
1124 p = trash.str;
1125 for (end = curr;
1126 end < url + ulen && *end != ']';
1127 end++, p++)
1128 *p = *end;
1129 if (*end != ']')
1130 return -1;
1131 *p = '\0';
1132
1133 /* Update out. */
1134 if (out) {
1135 out->host = curr;
1136 out->host_len = end - curr;
1137 }
1138
1139 /* Try IPv6 decoding. */
1140 if (!inet_pton(AF_INET6, trash.str, &((struct sockaddr_in6 *)addr)->sin6_addr))
1141 return -1;
1142 end++;
1143
1144 /* Decode port. */
1145 if (*end == ':') {
1146 end++;
1147 default_port = read_uint(&end, url + ulen);
1148 }
1149 ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port);
1150 ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
1151 return end - url;
1152 }
1153 else {
1154 /* We are looking for IP address. If you want to parse and
1155 * resolve hostname found in url, you can use str2sa_range(), but
1156 * be warned this can slow down global daemon performances
1157 * while handling lagging dns responses.
1158 */
1159 ret = url2ipv4(curr, &((struct sockaddr_in *)addr)->sin_addr);
1160 if (ret) {
1161 /* Update out. */
1162 if (out) {
1163 out->host = curr;
1164 out->host_len = ret;
1165 }
1166
1167 curr += ret;
1168
1169 /* Decode port. */
1170 if (*curr == ':') {
1171 curr++;
1172 default_port = read_uint(&curr, url + ulen);
1173 }
1174 ((struct sockaddr_in *)addr)->sin_port = htons(default_port);
1175
1176 /* Set family. */
1177 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
1178 return curr - url;
1179 }
1180 else if (global.mode & MODE_STARTING) {
1181 /* The IPv4 and IPv6 decoding fails, maybe the url contain name. Try to execute
1182 * synchronous DNS request only if HAProxy is in the start state.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001183 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001184
1185 /* look for : or / or end */
1186 for (end = curr;
1187 end < url + ulen && *end != '/' && *end != ':';
1188 end++);
1189 memcpy(trash.str, curr, end - curr);
1190 trash.str[end - curr] = '\0';
1191
1192 /* try to resolve an IPv4/IPv6 hostname */
1193 he = gethostbyname(trash.str);
1194 if (!he)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001195 return -1;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001196
1197 /* Update out. */
1198 if (out) {
1199 out->host = curr;
1200 out->host_len = end - curr;
1201 }
1202
1203 /* Decode port. */
1204 if (*end == ':') {
1205 end++;
1206 default_port = read_uint(&end, url + ulen);
1207 }
1208
1209 /* Copy IP address, set port and family. */
1210 switch (he->h_addrtype) {
1211 case AF_INET:
1212 ((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
1213 ((struct sockaddr_in *)addr)->sin_port = htons(default_port);
1214 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
1215 return end - url;
1216
1217 case AF_INET6:
1218 ((struct sockaddr_in6 *)addr)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
1219 ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port);
1220 ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
1221 return end - url;
1222 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001223 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001224 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001225 return -1;
1226}
1227
Willy Tarreau631f01c2011-09-05 00:36:48 +02001228/* Tries to convert a sockaddr_storage address to text form. Upon success, the
1229 * address family is returned so that it's easy for the caller to adapt to the
1230 * output format. Zero is returned if the address family is not supported. -1
1231 * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
1232 * supported.
1233 */
1234int addr_to_str(struct sockaddr_storage *addr, char *str, int size)
1235{
1236
1237 void *ptr;
1238
1239 if (size < 5)
1240 return 0;
1241 *str = '\0';
1242
1243 switch (addr->ss_family) {
1244 case AF_INET:
1245 ptr = &((struct sockaddr_in *)addr)->sin_addr;
1246 break;
1247 case AF_INET6:
1248 ptr = &((struct sockaddr_in6 *)addr)->sin6_addr;
1249 break;
1250 case AF_UNIX:
1251 memcpy(str, "unix", 5);
1252 return addr->ss_family;
1253 default:
1254 return 0;
1255 }
1256
1257 if (inet_ntop(addr->ss_family, ptr, str, size))
1258 return addr->ss_family;
1259
1260 /* failed */
1261 return -1;
1262}
1263
Simon Horman75ab8bd2014-06-16 09:39:41 +09001264/* Tries to convert a sockaddr_storage port to text form. Upon success, the
1265 * address family is returned so that it's easy for the caller to adapt to the
1266 * output format. Zero is returned if the address family is not supported. -1
1267 * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
1268 * supported.
1269 */
1270int port_to_str(struct sockaddr_storage *addr, char *str, int size)
1271{
1272
1273 uint16_t port;
1274
1275
1276 if (size < 5)
1277 return 0;
1278 *str = '\0';
1279
1280 switch (addr->ss_family) {
1281 case AF_INET:
1282 port = ((struct sockaddr_in *)addr)->sin_port;
1283 break;
1284 case AF_INET6:
1285 port = ((struct sockaddr_in6 *)addr)->sin6_port;
1286 break;
1287 case AF_UNIX:
1288 memcpy(str, "unix", 5);
1289 return addr->ss_family;
1290 default:
1291 return 0;
1292 }
1293
1294 snprintf(str, size, "%u", ntohs(port));
1295 return addr->ss_family;
1296}
1297
Willy Tarreaubaaee002006-06-26 02:48:02 +02001298/* will try to encode the string <string> replacing all characters tagged in
1299 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
1300 * prefixed by <escape>, and will store the result between <start> (included)
1301 * and <stop> (excluded), and will always terminate the string with a '\0'
1302 * before <stop>. The position of the '\0' is returned if the conversion
1303 * completes. If bytes are missing between <start> and <stop>, then the
1304 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
1305 * cannot even be stored so we return <start> without writing the 0.
1306 * The input string must also be zero-terminated.
1307 */
1308const char hextab[16] = "0123456789ABCDEF";
1309char *encode_string(char *start, char *stop,
1310 const char escape, const fd_set *map,
1311 const char *string)
1312{
1313 if (start < stop) {
1314 stop--; /* reserve one byte for the final '\0' */
1315 while (start < stop && *string != '\0') {
1316 if (!FD_ISSET((unsigned char)(*string), map))
1317 *start++ = *string;
1318 else {
1319 if (start + 3 >= stop)
1320 break;
1321 *start++ = escape;
1322 *start++ = hextab[(*string >> 4) & 15];
1323 *start++ = hextab[*string & 15];
1324 }
1325 string++;
1326 }
1327 *start = '\0';
1328 }
1329 return start;
1330}
1331
Thierry FOURNIERe059ec92014-03-17 12:01:13 +01001332/*
1333 * Same behavior as encode_string() above, except that it encodes chunk
1334 * <chunk> instead of a string.
1335 */
1336char *encode_chunk(char *start, char *stop,
1337 const char escape, const fd_set *map,
1338 const struct chunk *chunk)
1339{
1340 char *str = chunk->str;
1341 char *end = chunk->str + chunk->len;
1342
1343 if (start < stop) {
1344 stop--; /* reserve one byte for the final '\0' */
1345 while (start < stop && str < end) {
1346 if (!FD_ISSET((unsigned char)(*str), map))
1347 *start++ = *str;
1348 else {
1349 if (start + 3 >= stop)
1350 break;
1351 *start++ = escape;
1352 *start++ = hextab[(*str >> 4) & 15];
1353 *start++ = hextab[*str & 15];
1354 }
1355 str++;
1356 }
1357 *start = '\0';
1358 }
1359 return start;
1360}
1361
Thierry FOURNIERddea6262015-05-28 16:00:28 +02001362/* Check a string for using it in a CSV output format. If the string contains
1363 * one of the following four char <">, <,>, CR or LF, the string is
1364 * encapsulated between <"> and the <"> are escaped by a <""> sequence.
1365 * <str> is the input string to be escaped. The function assumes that
1366 * the input string is null-terminated.
1367 *
1368 * If <quote> is 0, the result is returned escaped but without double quote.
1369 * Is it useful if the escaped string is used between double quotes in the
1370 * format.
1371 *
1372 * printf("..., \"%s\", ...\r\n", csv_enc(str, 0));
1373 *
1374 * If the <quote> is 1, the converter put the quotes only if any character is
1375 * escaped. If the <quote> is 2, the converter put always the quotes.
1376 *
1377 * <output> is a struct chunk used for storing the output string if any
1378 * change will be done.
1379 *
1380 * The function returns the converted string on this output. If an error
1381 * occurs, the function return an empty string. This type of output is useful
1382 * for using the function directly as printf() argument.
1383 *
1384 * If the output buffer is too short to contain the input string, the result
1385 * is truncated.
1386 */
1387const char *csv_enc(const char *str, int quote, struct chunk *output)
1388{
1389 char *end = output->str + output->size;
1390 char *out = output->str + 1; /* +1 for reserving space for a first <"> */
1391
1392 while (*str && out < end - 2) { /* -2 for reserving space for <"> and \0. */
1393 *out = *str;
1394 if (*str == '"') {
1395 if (quote == 1)
1396 quote = 2;
1397 out++;
1398 if (out >= end - 2) {
1399 out--;
1400 break;
1401 }
1402 *out = '"';
1403 }
1404 if (quote == 1 && ( *str == '\r' || *str == '\n' || *str == ',') )
1405 quote = 2;
1406 out++;
1407 str++;
1408 }
1409
1410 if (quote == 1)
1411 quote = 0;
1412
1413 if (!quote) {
1414 *out = '\0';
1415 return output->str + 1;
1416 }
1417
1418 /* else quote == 2 */
1419 *output->str = '"';
1420 *out = '"';
1421 out++;
1422 *out = '\0';
1423 return output->str;
1424}
1425
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001426/* Decode an URL-encoded string in-place. The resulting string might
1427 * be shorter. If some forbidden characters are found, the conversion is
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001428 * aborted, the string is truncated before the issue and a negative value is
1429 * returned, otherwise the operation returns the length of the decoded string.
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001430 */
1431int url_decode(char *string)
1432{
1433 char *in, *out;
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001434 int ret = -1;
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001435
1436 in = string;
1437 out = string;
1438 while (*in) {
1439 switch (*in) {
1440 case '+' :
1441 *out++ = ' ';
1442 break;
1443 case '%' :
1444 if (!ishex(in[1]) || !ishex(in[2]))
1445 goto end;
1446 *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]);
1447 in += 2;
1448 break;
1449 default:
1450 *out++ = *in;
1451 break;
1452 }
1453 in++;
1454 }
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001455 ret = out - string; /* success */
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001456 end:
1457 *out = 0;
1458 return ret;
1459}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001460
Willy Tarreau6911fa42007-03-04 18:06:08 +01001461unsigned int str2ui(const char *s)
1462{
1463 return __str2ui(s);
1464}
1465
1466unsigned int str2uic(const char *s)
1467{
1468 return __str2uic(s);
1469}
1470
1471unsigned int strl2ui(const char *s, int len)
1472{
1473 return __strl2ui(s, len);
1474}
1475
1476unsigned int strl2uic(const char *s, int len)
1477{
1478 return __strl2uic(s, len);
1479}
1480
Willy Tarreau4ec83cd2010-10-15 23:19:55 +02001481unsigned int read_uint(const char **s, const char *end)
1482{
1483 return __read_uint(s, end);
1484}
1485
Thierry FOURNIER763a5d82015-07-06 23:09:52 +02001486/* This function reads an unsigned integer from the string pointed to by <s> and
1487 * returns it. The <s> pointer is adjusted to point to the first unread char. The
1488 * function automatically stops at <end>. If the number overflows, the 2^64-1
1489 * value is returned.
1490 */
1491unsigned long long int read_uint64(const char **s, const char *end)
1492{
1493 const char *ptr = *s;
1494 unsigned long long int i = 0, tmp;
1495 unsigned int j;
1496
1497 while (ptr < end) {
1498
1499 /* read next char */
1500 j = *ptr - '0';
1501 if (j > 9)
1502 goto read_uint64_end;
1503
1504 /* add char to the number and check overflow. */
1505 tmp = i * 10;
1506 if (tmp / 10 != i) {
1507 i = ULLONG_MAX;
1508 goto read_uint64_eat;
1509 }
1510 if (ULLONG_MAX - tmp < j) {
1511 i = ULLONG_MAX;
1512 goto read_uint64_eat;
1513 }
1514 i = tmp + j;
1515 ptr++;
1516 }
1517read_uint64_eat:
1518 /* eat each numeric char */
1519 while (ptr < end) {
1520 if ((unsigned int)(*ptr - '0') > 9)
1521 break;
1522 ptr++;
1523 }
1524read_uint64_end:
1525 *s = ptr;
1526 return i;
1527}
1528
1529/* This function reads an integer from the string pointed to by <s> and returns
1530 * it. The <s> pointer is adjusted to point to the first unread char. The function
1531 * automatically stops at <end>. Il the number is bigger than 2^63-2, the 2^63-1
1532 * value is returned. If the number is lowest than -2^63-1, the -2^63 value is
1533 * returned.
1534 */
1535long long int read_int64(const char **s, const char *end)
1536{
1537 unsigned long long int i = 0;
1538 int neg = 0;
1539
1540 /* Look for minus char. */
1541 if (**s == '-') {
1542 neg = 1;
1543 (*s)++;
1544 }
1545 else if (**s == '+')
1546 (*s)++;
1547
1548 /* convert as positive number. */
1549 i = read_uint64(s, end);
1550
1551 if (neg) {
1552 if (i > 0x8000000000000000ULL)
1553 return LLONG_MIN;
1554 return -i;
1555 }
1556 if (i > 0x7fffffffffffffffULL)
1557 return LLONG_MAX;
1558 return i;
1559}
1560
Willy Tarreau6911fa42007-03-04 18:06:08 +01001561/* This one is 7 times faster than strtol() on athlon with checks.
1562 * It returns the value of the number composed of all valid digits read,
1563 * and can process negative numbers too.
1564 */
1565int strl2ic(const char *s, int len)
1566{
1567 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001568 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001569
1570 if (len > 0) {
1571 if (*s != '-') {
1572 /* positive number */
1573 while (len-- > 0) {
1574 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001575 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001576 if (j > 9)
1577 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001578 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001579 }
1580 } else {
1581 /* negative number */
1582 s++;
1583 while (--len > 0) {
1584 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001585 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001586 if (j > 9)
1587 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001588 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001589 }
1590 }
1591 }
1592 return i;
1593}
1594
1595
1596/* This function reads exactly <len> chars from <s> and converts them to a
1597 * signed integer which it stores into <ret>. It accurately detects any error
1598 * (truncated string, invalid chars, overflows). It is meant to be used in
1599 * applications designed for hostile environments. It returns zero when the
1600 * number has successfully been converted, non-zero otherwise. When an error
1601 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
1602 * faster than strtol().
1603 */
1604int strl2irc(const char *s, int len, int *ret)
1605{
1606 int i = 0;
1607 int j;
1608
1609 if (!len)
1610 return 1;
1611
1612 if (*s != '-') {
1613 /* positive number */
1614 while (len-- > 0) {
1615 j = (*s++) - '0';
1616 if (j > 9) return 1; /* invalid char */
1617 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
1618 i = i * 10;
1619 if (i + j < i) return 1; /* check for addition overflow */
1620 i = i + j;
1621 }
1622 } else {
1623 /* negative number */
1624 s++;
1625 while (--len > 0) {
1626 j = (*s++) - '0';
1627 if (j > 9) return 1; /* invalid char */
1628 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
1629 i = i * 10;
1630 if (i - j > i) return 1; /* check for subtract overflow */
1631 i = i - j;
1632 }
1633 }
1634 *ret = i;
1635 return 0;
1636}
1637
1638
1639/* This function reads exactly <len> chars from <s> and converts them to a
1640 * signed integer which it stores into <ret>. It accurately detects any error
1641 * (truncated string, invalid chars, overflows). It is meant to be used in
1642 * applications designed for hostile environments. It returns zero when the
1643 * number has successfully been converted, non-zero otherwise. When an error
1644 * is returned, the <ret> value is left untouched. It is about 3 times slower
1645 * than str2irc().
1646 */
Willy Tarreau6911fa42007-03-04 18:06:08 +01001647
1648int strl2llrc(const char *s, int len, long long *ret)
1649{
1650 long long i = 0;
1651 int j;
1652
1653 if (!len)
1654 return 1;
1655
1656 if (*s != '-') {
1657 /* positive number */
1658 while (len-- > 0) {
1659 j = (*s++) - '0';
1660 if (j > 9) return 1; /* invalid char */
1661 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
1662 i = i * 10LL;
1663 if (i + j < i) return 1; /* check for addition overflow */
1664 i = i + j;
1665 }
1666 } else {
1667 /* negative number */
1668 s++;
1669 while (--len > 0) {
1670 j = (*s++) - '0';
1671 if (j > 9) return 1; /* invalid char */
1672 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
1673 i = i * 10LL;
1674 if (i - j > i) return 1; /* check for subtract overflow */
1675 i = i - j;
1676 }
1677 }
1678 *ret = i;
1679 return 0;
1680}
1681
Thierry FOURNIER511e9472014-01-23 17:40:34 +01001682/* This function is used with pat_parse_dotted_ver(). It converts a string
1683 * composed by two number separated by a dot. Each part must contain in 16 bits
1684 * because internally they will be represented as a 32-bit quantity stored in
1685 * a 64-bit integer. It returns zero when the number has successfully been
1686 * converted, non-zero otherwise. When an error is returned, the <ret> value
1687 * is left untouched.
1688 *
1689 * "1.3" -> 0x0000000000010003
1690 * "65535.65535" -> 0x00000000ffffffff
1691 */
1692int strl2llrc_dotted(const char *text, int len, long long *ret)
1693{
1694 const char *end = &text[len];
1695 const char *p;
1696 long long major, minor;
1697
1698 /* Look for dot. */
1699 for (p = text; p < end; p++)
1700 if (*p == '.')
1701 break;
1702
1703 /* Convert major. */
1704 if (strl2llrc(text, p - text, &major) != 0)
1705 return 1;
1706
1707 /* Check major. */
1708 if (major >= 65536)
1709 return 1;
1710
1711 /* Convert minor. */
1712 minor = 0;
1713 if (p < end)
1714 if (strl2llrc(p + 1, end - (p + 1), &minor) != 0)
1715 return 1;
1716
1717 /* Check minor. */
1718 if (minor >= 65536)
1719 return 1;
1720
1721 /* Compose value. */
1722 *ret = (major << 16) | (minor & 0xffff);
1723 return 0;
1724}
1725
Willy Tarreaua0d37b62007-12-02 22:00:35 +01001726/* This function parses a time value optionally followed by a unit suffix among
1727 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
1728 * expected by the caller. The computation does its best to avoid overflows.
1729 * The value is returned in <ret> if everything is fine, and a NULL is returned
1730 * by the function. In case of error, a pointer to the error is returned and
1731 * <ret> is left untouched. Values are automatically rounded up when needed.
1732 */
1733const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
1734{
1735 unsigned imult, idiv;
1736 unsigned omult, odiv;
1737 unsigned value;
1738
1739 omult = odiv = 1;
1740
1741 switch (unit_flags & TIME_UNIT_MASK) {
1742 case TIME_UNIT_US: omult = 1000000; break;
1743 case TIME_UNIT_MS: omult = 1000; break;
1744 case TIME_UNIT_S: break;
1745 case TIME_UNIT_MIN: odiv = 60; break;
1746 case TIME_UNIT_HOUR: odiv = 3600; break;
1747 case TIME_UNIT_DAY: odiv = 86400; break;
1748 default: break;
1749 }
1750
1751 value = 0;
1752
1753 while (1) {
1754 unsigned int j;
1755
1756 j = *text - '0';
1757 if (j > 9)
1758 break;
1759 text++;
1760 value *= 10;
1761 value += j;
1762 }
1763
1764 imult = idiv = 1;
1765 switch (*text) {
1766 case '\0': /* no unit = default unit */
1767 imult = omult = idiv = odiv = 1;
1768 break;
1769 case 's': /* second = unscaled unit */
1770 break;
1771 case 'u': /* microsecond : "us" */
1772 if (text[1] == 's') {
1773 idiv = 1000000;
1774 text++;
1775 }
1776 break;
1777 case 'm': /* millisecond : "ms" or minute: "m" */
1778 if (text[1] == 's') {
1779 idiv = 1000;
1780 text++;
1781 } else
1782 imult = 60;
1783 break;
1784 case 'h': /* hour : "h" */
1785 imult = 3600;
1786 break;
1787 case 'd': /* day : "d" */
1788 imult = 86400;
1789 break;
1790 default:
1791 return text;
1792 break;
1793 }
1794
1795 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
1796 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
1797 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
1798 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
1799
1800 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
1801 *ret = value;
1802 return NULL;
1803}
Willy Tarreau6911fa42007-03-04 18:06:08 +01001804
Emeric Brun39132b22010-01-04 14:57:24 +01001805/* this function converts the string starting at <text> to an unsigned int
1806 * stored in <ret>. If an error is detected, the pointer to the unexpected
1807 * character is returned. If the conversio is succesful, NULL is returned.
1808 */
1809const char *parse_size_err(const char *text, unsigned *ret) {
1810 unsigned value = 0;
1811
1812 while (1) {
1813 unsigned int j;
1814
1815 j = *text - '0';
1816 if (j > 9)
1817 break;
1818 if (value > ~0U / 10)
1819 return text;
1820 value *= 10;
1821 if (value > (value + j))
1822 return text;
1823 value += j;
1824 text++;
1825 }
1826
1827 switch (*text) {
1828 case '\0':
1829 break;
1830 case 'K':
1831 case 'k':
1832 if (value > ~0U >> 10)
1833 return text;
1834 value = value << 10;
1835 break;
1836 case 'M':
1837 case 'm':
1838 if (value > ~0U >> 20)
1839 return text;
1840 value = value << 20;
1841 break;
1842 case 'G':
1843 case 'g':
1844 if (value > ~0U >> 30)
1845 return text;
1846 value = value << 30;
1847 break;
1848 default:
1849 return text;
1850 }
1851
Godbach58048a22015-01-28 17:36:16 +08001852 if (*text != '\0' && *++text != '\0')
1853 return text;
1854
Emeric Brun39132b22010-01-04 14:57:24 +01001855 *ret = value;
1856 return NULL;
1857}
1858
Willy Tarreau126d4062013-12-03 17:50:47 +01001859/*
1860 * Parse binary string written in hexadecimal (source) and store the decoded
1861 * result into binstr and set binstrlen to the lengh of binstr. Memory for
1862 * binstr is allocated by the function. In case of error, returns 0 with an
Thierry FOURNIERee330af2014-01-21 11:36:14 +01001863 * error message in err. In succes case, it returns the consumed length.
Willy Tarreau126d4062013-12-03 17:50:47 +01001864 */
1865int parse_binary(const char *source, char **binstr, int *binstrlen, char **err)
1866{
1867 int len;
1868 const char *p = source;
1869 int i,j;
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001870 int alloc;
Willy Tarreau126d4062013-12-03 17:50:47 +01001871
1872 len = strlen(source);
1873 if (len % 2) {
1874 memprintf(err, "an even number of hex digit is expected");
1875 return 0;
1876 }
1877
1878 len = len >> 1;
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001879
Willy Tarreau126d4062013-12-03 17:50:47 +01001880 if (!*binstr) {
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001881 *binstr = calloc(len, sizeof(char));
1882 if (!*binstr) {
1883 memprintf(err, "out of memory while loading string pattern");
1884 return 0;
1885 }
1886 alloc = 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001887 }
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001888 else {
1889 if (*binstrlen < len) {
1890 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
1891 len, *binstrlen);
1892 return 0;
1893 }
1894 alloc = 0;
1895 }
1896 *binstrlen = len;
Willy Tarreau126d4062013-12-03 17:50:47 +01001897
1898 i = j = 0;
1899 while (j < len) {
1900 if (!ishex(p[i++]))
1901 goto bad_input;
1902 if (!ishex(p[i++]))
1903 goto bad_input;
1904 (*binstr)[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
1905 }
Thierry FOURNIERee330af2014-01-21 11:36:14 +01001906 return len << 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001907
1908bad_input:
1909 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001910 if (alloc)
1911 free(binstr);
Willy Tarreau126d4062013-12-03 17:50:47 +01001912 return 0;
1913}
1914
Willy Tarreau946ba592009-05-10 15:41:18 +02001915/* copies at most <n> characters from <src> and always terminates with '\0' */
1916char *my_strndup(const char *src, int n)
1917{
1918 int len = 0;
1919 char *ret;
1920
1921 while (len < n && src[len])
1922 len++;
1923
1924 ret = (char *)malloc(len + 1);
1925 if (!ret)
1926 return ret;
1927 memcpy(ret, src, len);
1928 ret[len] = '\0';
1929 return ret;
1930}
1931
Baptiste Assmannbb77c8e2013-10-06 23:24:13 +02001932/*
1933 * search needle in haystack
1934 * returns the pointer if found, returns NULL otherwise
1935 */
1936const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen)
1937{
1938 const void *c = NULL;
1939 unsigned char f;
1940
1941 if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen))
1942 return NULL;
1943
1944 f = *(char *)needle;
1945 c = haystack;
1946 while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) {
1947 if ((haystacklen - (c - haystack)) < needlelen)
1948 return NULL;
1949
1950 if (memcmp(c, needle, needlelen) == 0)
1951 return c;
1952 ++c;
1953 }
1954 return NULL;
1955}
1956
Willy Tarreau482b00d2009-10-04 22:48:42 +02001957/* This function returns the first unused key greater than or equal to <key> in
1958 * ID tree <root>. Zero is returned if no place is found.
1959 */
1960unsigned int get_next_id(struct eb_root *root, unsigned int key)
1961{
1962 struct eb32_node *used;
1963
1964 do {
1965 used = eb32_lookup_ge(root, key);
1966 if (!used || used->key > key)
1967 return key; /* key is available */
1968 key++;
1969 } while (key);
1970 return key;
1971}
1972
Willy Tarreau348238b2010-01-18 15:05:57 +01001973/* This function compares a sample word possibly followed by blanks to another
1974 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
1975 * otherwise zero. This intends to be used when checking HTTP headers for some
1976 * values. Note that it validates a word followed only by blanks but does not
1977 * validate a word followed by blanks then other chars.
1978 */
1979int word_match(const char *sample, int slen, const char *word, int wlen)
1980{
1981 if (slen < wlen)
1982 return 0;
1983
1984 while (wlen) {
1985 char c = *sample ^ *word;
1986 if (c && c != ('A' ^ 'a'))
1987 return 0;
1988 sample++;
1989 word++;
1990 slen--;
1991 wlen--;
1992 }
1993
1994 while (slen) {
1995 if (*sample != ' ' && *sample != '\t')
1996 return 0;
1997 sample++;
1998 slen--;
1999 }
2000 return 1;
2001}
Willy Tarreau482b00d2009-10-04 22:48:42 +02002002
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02002003/* Converts any text-formatted IPv4 address to a host-order IPv4 address. It
2004 * is particularly fast because it avoids expensive operations such as
2005 * multiplies, which are optimized away at the end. It requires a properly
2006 * formated address though (3 points).
2007 */
2008unsigned int inetaddr_host(const char *text)
2009{
2010 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
2011 register unsigned int dig100, dig10, dig1;
2012 int s;
2013 const char *p, *d;
2014
2015 dig1 = dig10 = dig100 = ascii_zero;
2016 s = 24;
2017
2018 p = text;
2019 while (1) {
2020 if (((unsigned)(*p - '0')) <= 9) {
2021 p++;
2022 continue;
2023 }
2024
2025 /* here, we have a complete byte between <text> and <p> (exclusive) */
2026 if (p == text)
2027 goto end;
2028
2029 d = p - 1;
2030 dig1 |= (unsigned int)(*d << s);
2031 if (d == text)
2032 goto end;
2033
2034 d--;
2035 dig10 |= (unsigned int)(*d << s);
2036 if (d == text)
2037 goto end;
2038
2039 d--;
2040 dig100 |= (unsigned int)(*d << s);
2041 end:
2042 if (!s || *p != '.')
2043 break;
2044
2045 s -= 8;
2046 text = ++p;
2047 }
2048
2049 dig100 -= ascii_zero;
2050 dig10 -= ascii_zero;
2051 dig1 -= ascii_zero;
2052 return ((dig100 * 10) + dig10) * 10 + dig1;
2053}
2054
2055/*
2056 * Idem except the first unparsed character has to be passed in <stop>.
2057 */
2058unsigned int inetaddr_host_lim(const char *text, const char *stop)
2059{
2060 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
2061 register unsigned int dig100, dig10, dig1;
2062 int s;
2063 const char *p, *d;
2064
2065 dig1 = dig10 = dig100 = ascii_zero;
2066 s = 24;
2067
2068 p = text;
2069 while (1) {
2070 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
2071 p++;
2072 continue;
2073 }
2074
2075 /* here, we have a complete byte between <text> and <p> (exclusive) */
2076 if (p == text)
2077 goto end;
2078
2079 d = p - 1;
2080 dig1 |= (unsigned int)(*d << s);
2081 if (d == text)
2082 goto end;
2083
2084 d--;
2085 dig10 |= (unsigned int)(*d << s);
2086 if (d == text)
2087 goto end;
2088
2089 d--;
2090 dig100 |= (unsigned int)(*d << s);
2091 end:
2092 if (!s || p == stop || *p != '.')
2093 break;
2094
2095 s -= 8;
2096 text = ++p;
2097 }
2098
2099 dig100 -= ascii_zero;
2100 dig10 -= ascii_zero;
2101 dig1 -= ascii_zero;
2102 return ((dig100 * 10) + dig10) * 10 + dig1;
2103}
2104
2105/*
2106 * Idem except the pointer to first unparsed byte is returned into <ret> which
2107 * must not be NULL.
2108 */
Willy Tarreau74172752010-10-15 23:21:42 +02002109unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret)
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02002110{
2111 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
2112 register unsigned int dig100, dig10, dig1;
2113 int s;
Willy Tarreau74172752010-10-15 23:21:42 +02002114 char *p, *d;
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02002115
2116 dig1 = dig10 = dig100 = ascii_zero;
2117 s = 24;
2118
2119 p = text;
2120 while (1) {
2121 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
2122 p++;
2123 continue;
2124 }
2125
2126 /* here, we have a complete byte between <text> and <p> (exclusive) */
2127 if (p == text)
2128 goto end;
2129
2130 d = p - 1;
2131 dig1 |= (unsigned int)(*d << s);
2132 if (d == text)
2133 goto end;
2134
2135 d--;
2136 dig10 |= (unsigned int)(*d << s);
2137 if (d == text)
2138 goto end;
2139
2140 d--;
2141 dig100 |= (unsigned int)(*d << s);
2142 end:
2143 if (!s || p == stop || *p != '.')
2144 break;
2145
2146 s -= 8;
2147 text = ++p;
2148 }
2149
2150 *ret = p;
2151 dig100 -= ascii_zero;
2152 dig10 -= ascii_zero;
2153 dig1 -= ascii_zero;
2154 return ((dig100 * 10) + dig10) * 10 + dig1;
2155}
2156
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02002157/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
2158 * or the number of chars read in case of success. Maybe this could be replaced
2159 * by one of the functions above. Also, apparently this function does not support
2160 * hosts above 255 and requires exactly 4 octets.
Willy Tarreau075415a2013-12-12 11:29:39 +01002161 * The destination is only modified on success.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02002162 */
2163int buf2ip(const char *buf, size_t len, struct in_addr *dst)
2164{
2165 const char *addr;
2166 int saw_digit, octets, ch;
2167 u_char tmp[4], *tp;
2168 const char *cp = buf;
2169
2170 saw_digit = 0;
2171 octets = 0;
2172 *(tp = tmp) = 0;
2173
2174 for (addr = buf; addr - buf < len; addr++) {
2175 unsigned char digit = (ch = *addr) - '0';
2176
2177 if (digit > 9 && ch != '.')
2178 break;
2179
2180 if (digit <= 9) {
2181 u_int new = *tp * 10 + digit;
2182
2183 if (new > 255)
2184 return 0;
2185
2186 *tp = new;
2187
2188 if (!saw_digit) {
2189 if (++octets > 4)
2190 return 0;
2191 saw_digit = 1;
2192 }
2193 } else if (ch == '.' && saw_digit) {
2194 if (octets == 4)
2195 return 0;
2196
2197 *++tp = 0;
2198 saw_digit = 0;
2199 } else
2200 return 0;
2201 }
2202
2203 if (octets < 4)
2204 return 0;
2205
2206 memcpy(&dst->s_addr, tmp, 4);
2207 return addr - cp;
2208}
2209
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002210/* This function converts the string in <buf> of the len <len> to
2211 * struct in6_addr <dst> which must be allocated by the caller.
2212 * This function returns 1 in success case, otherwise zero.
Willy Tarreau075415a2013-12-12 11:29:39 +01002213 * The destination is only modified on success.
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002214 */
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002215int buf2ip6(const char *buf, size_t len, struct in6_addr *dst)
2216{
Thierry FOURNIERcd659912013-12-11 12:33:54 +01002217 char null_term_ip6[INET6_ADDRSTRLEN + 1];
Willy Tarreau075415a2013-12-12 11:29:39 +01002218 struct in6_addr out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002219
Thierry FOURNIERcd659912013-12-11 12:33:54 +01002220 if (len > INET6_ADDRSTRLEN)
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002221 return 0;
2222
2223 memcpy(null_term_ip6, buf, len);
2224 null_term_ip6[len] = '\0';
2225
Willy Tarreau075415a2013-12-12 11:29:39 +01002226 if (!inet_pton(AF_INET6, null_term_ip6, &out))
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002227 return 0;
2228
Willy Tarreau075415a2013-12-12 11:29:39 +01002229 *dst = out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002230 return 1;
2231}
2232
Willy Tarreauacf95772010-06-14 19:09:21 +02002233/* To be used to quote config arg positions. Returns the short string at <ptr>
2234 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
2235 * if ptr is NULL or empty. The string is locally allocated.
2236 */
2237const char *quote_arg(const char *ptr)
2238{
2239 static char val[32];
2240 int i;
2241
2242 if (!ptr || !*ptr)
2243 return "end of line";
2244 val[0] = '\'';
Willy Tarreaude2dd6b2013-01-24 02:14:42 +01002245 for (i = 1; i < sizeof(val) - 2 && *ptr; i++)
Willy Tarreauacf95772010-06-14 19:09:21 +02002246 val[i] = *ptr++;
2247 val[i++] = '\'';
2248 val[i] = '\0';
2249 return val;
2250}
2251
Willy Tarreau5b180202010-07-18 10:40:48 +02002252/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
2253int get_std_op(const char *str)
2254{
2255 int ret = -1;
2256
2257 if (*str == 'e' && str[1] == 'q')
2258 ret = STD_OP_EQ;
2259 else if (*str == 'n' && str[1] == 'e')
2260 ret = STD_OP_NE;
2261 else if (*str == 'l') {
2262 if (str[1] == 'e') ret = STD_OP_LE;
2263 else if (str[1] == 't') ret = STD_OP_LT;
2264 }
2265 else if (*str == 'g') {
2266 if (str[1] == 'e') ret = STD_OP_GE;
2267 else if (str[1] == 't') ret = STD_OP_GT;
2268 }
2269
2270 if (ret == -1 || str[2] != '\0')
2271 return -1;
2272 return ret;
2273}
2274
Willy Tarreau4c14eaa2010-11-24 14:01:45 +01002275/* hash a 32-bit integer to another 32-bit integer */
2276unsigned int full_hash(unsigned int a)
2277{
2278 return __full_hash(a);
2279}
2280
David du Colombier4f92d322011-03-24 11:09:31 +01002281/* Return non-zero if IPv4 address is part of the network,
2282 * otherwise zero.
2283 */
2284int in_net_ipv4(struct in_addr *addr, struct in_addr *mask, struct in_addr *net)
2285{
2286 return((addr->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr));
2287}
2288
2289/* Return non-zero if IPv6 address is part of the network,
2290 * otherwise zero.
2291 */
2292int in_net_ipv6(struct in6_addr *addr, struct in6_addr *mask, struct in6_addr *net)
2293{
2294 int i;
2295
2296 for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++)
2297 if (((((int *)addr)[i] & ((int *)mask)[i])) !=
2298 (((int *)net)[i] & ((int *)mask)[i]))
2299 return 0;
2300 return 1;
2301}
2302
2303/* RFC 4291 prefix */
2304const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00,
2305 0x00, 0x00, 0x00, 0x00,
2306 0x00, 0x00, 0xFF, 0xFF };
2307
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002308/* Map IPv4 adress on IPv6 address, as specified in RFC 3513.
2309 * Input and output may overlap.
2310 */
David du Colombier4f92d322011-03-24 11:09:31 +01002311void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr)
2312{
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002313 struct in_addr tmp_addr;
2314
2315 tmp_addr.s_addr = sin_addr->s_addr;
David du Colombier4f92d322011-03-24 11:09:31 +01002316 memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx));
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002317 memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4);
David du Colombier4f92d322011-03-24 11:09:31 +01002318}
2319
2320/* Map IPv6 adress on IPv4 address, as specified in RFC 3513.
2321 * Return true if conversion is possible and false otherwise.
2322 */
2323int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr)
2324{
2325 if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) {
2326 memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]),
2327 sizeof(struct in_addr));
2328 return 1;
2329 }
2330
2331 return 0;
2332}
2333
William Lallemand421f5b52012-02-06 18:15:57 +01002334char *human_time(int t, short hz_div) {
2335 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
2336 char *p = rv;
Willy Tarreau761b3d52014-04-14 14:53:06 +02002337 char *end = rv + sizeof(rv);
William Lallemand421f5b52012-02-06 18:15:57 +01002338 int cnt=2; // print two numbers
2339
2340 if (unlikely(t < 0 || hz_div <= 0)) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002341 snprintf(p, end - p, "?");
William Lallemand421f5b52012-02-06 18:15:57 +01002342 return rv;
2343 }
2344
2345 if (unlikely(hz_div > 1))
2346 t /= hz_div;
2347
2348 if (t >= DAY) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002349 p += snprintf(p, end - p, "%dd", t / DAY);
William Lallemand421f5b52012-02-06 18:15:57 +01002350 cnt--;
2351 }
2352
2353 if (cnt && t % DAY / HOUR) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002354 p += snprintf(p, end - p, "%dh", t % DAY / HOUR);
William Lallemand421f5b52012-02-06 18:15:57 +01002355 cnt--;
2356 }
2357
2358 if (cnt && t % HOUR / MINUTE) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002359 p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE);
William Lallemand421f5b52012-02-06 18:15:57 +01002360 cnt--;
2361 }
2362
2363 if ((cnt && t % MINUTE) || !t) // also display '0s'
Willy Tarreau761b3d52014-04-14 14:53:06 +02002364 p += snprintf(p, end - p, "%ds", t % MINUTE / SEC);
William Lallemand421f5b52012-02-06 18:15:57 +01002365
2366 return rv;
2367}
2368
2369const char *monthname[12] = {
2370 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2371 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2372};
2373
2374/* date2str_log: write a date in the format :
2375 * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d",
2376 * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2377 * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000);
2378 *
2379 * without using sprintf. return a pointer to the last char written (\0) or
2380 * NULL if there isn't enough space.
2381 */
2382char *date2str_log(char *dst, struct tm *tm, struct timeval *date, size_t size)
2383{
2384
2385 if (size < 25) /* the size is fixed: 24 chars + \0 */
2386 return NULL;
2387
2388 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2389 *dst++ = '/';
2390 memcpy(dst, monthname[tm->tm_mon], 3); // month
2391 dst += 3;
2392 *dst++ = '/';
2393 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2394 *dst++ = ':';
2395 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2396 *dst++ = ':';
2397 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2398 *dst++ = ':';
2399 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2400 *dst++ = '.';
2401 utoa_pad((unsigned int)(date->tv_usec/1000), dst, 4); // millisecondes
2402 dst += 3; // only the 3 first digits
2403 *dst = '\0';
2404
2405 return dst;
2406}
2407
2408/* gmt2str_log: write a date in the format :
2409 * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf
2410 * return a pointer to the last char written (\0) or
2411 * NULL if there isn't enough space.
2412 */
2413char *gmt2str_log(char *dst, struct tm *tm, size_t size)
2414{
Yuxans Yao4e25b012012-10-19 10:36:09 +08002415 if (size < 27) /* the size is fixed: 26 chars + \0 */
William Lallemand421f5b52012-02-06 18:15:57 +01002416 return NULL;
2417
2418 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2419 *dst++ = '/';
2420 memcpy(dst, monthname[tm->tm_mon], 3); // month
2421 dst += 3;
2422 *dst++ = '/';
2423 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2424 *dst++ = ':';
2425 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2426 *dst++ = ':';
2427 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2428 *dst++ = ':';
2429 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2430 *dst++ = ' ';
2431 *dst++ = '+';
2432 *dst++ = '0';
2433 *dst++ = '0';
2434 *dst++ = '0';
2435 *dst++ = '0';
2436 *dst = '\0';
2437
2438 return dst;
2439}
2440
Yuxans Yao4e25b012012-10-19 10:36:09 +08002441/* localdate2str_log: write a date in the format :
2442 * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf
2443 * * return a pointer to the last char written (\0) or
2444 * * NULL if there isn't enough space.
2445 */
2446char *localdate2str_log(char *dst, struct tm *tm, size_t size)
2447{
2448 if (size < 27) /* the size is fixed: 26 chars + \0 */
2449 return NULL;
2450
2451 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2452 *dst++ = '/';
2453 memcpy(dst, monthname[tm->tm_mon], 3); // month
2454 dst += 3;
2455 *dst++ = '/';
2456 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2457 *dst++ = ':';
2458 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2459 *dst++ = ':';
2460 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2461 *dst++ = ':';
2462 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2463 *dst++ = ' ';
2464 memcpy(dst, localtimezone, 5); // timezone
2465 dst += 5;
2466 *dst = '\0';
2467
2468 return dst;
2469}
2470
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002471/* Dynamically allocates a string of the proper length to hold the formatted
2472 * output. NULL is returned on error. The caller is responsible for freeing the
2473 * memory area using free(). The resulting string is returned in <out> if the
2474 * pointer is not NULL. A previous version of <out> might be used to build the
2475 * new string, and it will be freed before returning if it is not NULL, which
2476 * makes it possible to build complex strings from iterative calls without
2477 * having to care about freeing intermediate values, as in the example below :
2478 *
2479 * memprintf(&err, "invalid argument: '%s'", arg);
2480 * ...
2481 * memprintf(&err, "parser said : <%s>\n", *err);
2482 * ...
2483 * free(*err);
2484 *
2485 * This means that <err> must be initialized to NULL before first invocation.
2486 * The return value also holds the allocated string, which eases error checking
2487 * and immediate consumption. If the output pointer is not used, NULL must be
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002488 * passed instead and it will be ignored. The returned message will then also
2489 * be NULL so that the caller does not have to bother with freeing anything.
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002490 *
2491 * It is also convenient to use it without any free except the last one :
2492 * err = NULL;
2493 * if (!fct1(err)) report(*err);
2494 * if (!fct2(err)) report(*err);
2495 * if (!fct3(err)) report(*err);
2496 * free(*err);
2497 */
2498char *memprintf(char **out, const char *format, ...)
2499{
2500 va_list args;
2501 char *ret = NULL;
2502 int allocated = 0;
2503 int needed = 0;
2504
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002505 if (!out)
2506 return NULL;
2507
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002508 do {
2509 /* vsnprintf() will return the required length even when the
2510 * target buffer is NULL. We do this in a loop just in case
2511 * intermediate evaluations get wrong.
2512 */
2513 va_start(args, format);
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002514 needed = vsnprintf(ret, allocated, format, args);
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002515 va_end(args);
2516
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002517 if (needed < allocated) {
2518 /* Note: on Solaris 8, the first iteration always
2519 * returns -1 if allocated is zero, so we force a
2520 * retry.
2521 */
2522 if (!allocated)
2523 needed = 0;
2524 else
2525 break;
2526 }
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002527
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002528 allocated = needed + 1;
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002529 ret = realloc(ret, allocated);
2530 } while (ret);
2531
2532 if (needed < 0) {
2533 /* an error was encountered */
2534 free(ret);
2535 ret = NULL;
2536 }
2537
2538 if (out) {
2539 free(*out);
2540 *out = ret;
2541 }
2542
2543 return ret;
2544}
William Lallemand421f5b52012-02-06 18:15:57 +01002545
Willy Tarreau21c705b2012-09-14 11:40:36 +02002546/* Used to add <level> spaces before each line of <out>, unless there is only one line.
2547 * The input argument is automatically freed and reassigned. The result will have to be
Willy Tarreau70eec382012-10-10 08:56:47 +02002548 * freed by the caller. It also supports being passed a NULL which results in the same
2549 * output.
Willy Tarreau21c705b2012-09-14 11:40:36 +02002550 * Example of use :
2551 * parse(cmd, &err); (callee: memprintf(&err, ...))
2552 * fprintf(stderr, "Parser said: %s\n", indent_error(&err));
2553 * free(err);
2554 */
2555char *indent_msg(char **out, int level)
2556{
2557 char *ret, *in, *p;
2558 int needed = 0;
2559 int lf = 0;
2560 int lastlf = 0;
2561 int len;
2562
Willy Tarreau70eec382012-10-10 08:56:47 +02002563 if (!out || !*out)
2564 return NULL;
2565
Willy Tarreau21c705b2012-09-14 11:40:36 +02002566 in = *out - 1;
2567 while ((in = strchr(in + 1, '\n')) != NULL) {
2568 lastlf = in - *out;
2569 lf++;
2570 }
2571
2572 if (!lf) /* single line, no LF, return it as-is */
2573 return *out;
2574
2575 len = strlen(*out);
2576
2577 if (lf == 1 && lastlf == len - 1) {
2578 /* single line, LF at end, strip it and return as-is */
2579 (*out)[lastlf] = 0;
2580 return *out;
2581 }
2582
2583 /* OK now we have at least one LF, we need to process the whole string
2584 * as a multi-line string. What we'll do :
2585 * - prefix with an LF if there is none
2586 * - add <level> spaces before each line
2587 * This means at most ( 1 + level + (len-lf) + lf*<1+level) ) =
2588 * 1 + level + len + lf * level = 1 + level * (lf + 1) + len.
2589 */
2590
2591 needed = 1 + level * (lf + 1) + len + 1;
2592 p = ret = malloc(needed);
2593 in = *out;
2594
2595 /* skip initial LFs */
2596 while (*in == '\n')
2597 in++;
2598
2599 /* copy each line, prefixed with LF and <level> spaces, and without the trailing LF */
2600 while (*in) {
2601 *p++ = '\n';
2602 memset(p, ' ', level);
2603 p += level;
2604 do {
2605 *p++ = *in++;
2606 } while (*in && *in != '\n');
2607 if (*in)
2608 in++;
2609 }
2610 *p = 0;
2611
2612 free(*out);
2613 *out = ret;
2614
2615 return ret;
2616}
2617
Willy Tarreaudad36a32013-03-11 01:20:04 +01002618/* Convert occurrences of environment variables in the input string to their
2619 * corresponding value. A variable is identified as a series of alphanumeric
2620 * characters or underscores following a '$' sign. The <in> string must be
2621 * free()able. NULL returns NULL. The resulting string might be reallocated if
2622 * some expansion is made. Variable names may also be enclosed into braces if
2623 * needed (eg: to concatenate alphanum characters).
2624 */
2625char *env_expand(char *in)
2626{
2627 char *txt_beg;
2628 char *out;
2629 char *txt_end;
2630 char *var_beg;
2631 char *var_end;
2632 char *value;
2633 char *next;
2634 int out_len;
2635 int val_len;
2636
2637 if (!in)
2638 return in;
2639
2640 value = out = NULL;
2641 out_len = 0;
2642
2643 txt_beg = in;
2644 do {
2645 /* look for next '$' sign in <in> */
2646 for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++);
2647
2648 if (!*txt_end && !out) /* end and no expansion performed */
2649 return in;
2650
2651 val_len = 0;
2652 next = txt_end;
2653 if (*txt_end == '$') {
2654 char save;
2655
2656 var_beg = txt_end + 1;
2657 if (*var_beg == '{')
2658 var_beg++;
2659
2660 var_end = var_beg;
2661 while (isalnum((int)(unsigned char)*var_end) || *var_end == '_') {
2662 var_end++;
2663 }
2664
2665 next = var_end;
2666 if (*var_end == '}' && (var_beg > txt_end + 1))
2667 next++;
2668
2669 /* get value of the variable name at this location */
2670 save = *var_end;
2671 *var_end = '\0';
2672 value = getenv(var_beg);
2673 *var_end = save;
2674 val_len = value ? strlen(value) : 0;
2675 }
2676
2677 out = realloc(out, out_len + (txt_end - txt_beg) + val_len + 1);
2678 if (txt_end > txt_beg) {
2679 memcpy(out + out_len, txt_beg, txt_end - txt_beg);
2680 out_len += txt_end - txt_beg;
2681 }
2682 if (val_len) {
2683 memcpy(out + out_len, value, val_len);
2684 out_len += val_len;
2685 }
2686 out[out_len] = 0;
2687 txt_beg = next;
2688 } while (*txt_beg);
2689
2690 /* here we know that <out> was allocated and that we don't need <in> anymore */
2691 free(in);
2692 return out;
2693}
2694
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002695
2696/* same as strstr() but case-insensitive and with limit length */
2697const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2)
2698{
2699 char *pptr, *sptr, *start;
Willy Tarreauc8746532014-05-28 23:05:07 +02002700 unsigned int slen, plen;
2701 unsigned int tmp1, tmp2;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002702
2703 if (str1 == NULL || len_str1 == 0) // search pattern into an empty string => search is not found
2704 return NULL;
2705
2706 if (str2 == NULL || len_str2 == 0) // pattern is empty => every str1 match
2707 return str1;
2708
2709 if (len_str1 < len_str2) // pattern is longer than string => search is not found
2710 return NULL;
2711
2712 for (tmp1 = 0, start = (char *)str1, pptr = (char *)str2, slen = len_str1, plen = len_str2; slen >= plen; start++, slen--) {
2713 while (toupper(*start) != toupper(*str2)) {
2714 start++;
2715 slen--;
2716 tmp1++;
2717
2718 if (tmp1 >= len_str1)
2719 return NULL;
2720
2721 /* if pattern longer than string */
2722 if (slen < plen)
2723 return NULL;
2724 }
2725
2726 sptr = start;
2727 pptr = (char *)str2;
2728
2729 tmp2 = 0;
2730 while (toupper(*sptr) == toupper(*pptr)) {
2731 sptr++;
2732 pptr++;
2733 tmp2++;
2734
2735 if (*pptr == '\0' || tmp2 == len_str2) /* end of pattern found */
2736 return start;
2737 if (*sptr == '\0' || tmp2 == len_str1) /* end of string found and the pattern is not fully found */
2738 return NULL;
2739 }
2740 }
2741 return NULL;
2742}
2743
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002744/* This function read the next valid utf8 char.
2745 * <s> is the byte srray to be decode, <len> is its length.
2746 * The function returns decoded char encoded like this:
2747 * The 4 msb are the return code (UTF8_CODE_*), the 4 lsb
2748 * are the length read. The decoded character is stored in <c>.
2749 */
2750unsigned char utf8_next(const char *s, int len, unsigned int *c)
2751{
2752 const unsigned char *p = (unsigned char *)s;
2753 int dec;
2754 unsigned char code = UTF8_CODE_OK;
2755
2756 if (len < 1)
2757 return UTF8_CODE_OK;
2758
2759 /* Check the type of UTF8 sequence
2760 *
2761 * 0... .... 0x00 <= x <= 0x7f : 1 byte: ascii char
2762 * 10.. .... 0x80 <= x <= 0xbf : invalid sequence
2763 * 110. .... 0xc0 <= x <= 0xdf : 2 bytes
2764 * 1110 .... 0xe0 <= x <= 0xef : 3 bytes
2765 * 1111 0... 0xf0 <= x <= 0xf7 : 4 bytes
2766 * 1111 10.. 0xf8 <= x <= 0xfb : 5 bytes
2767 * 1111 110. 0xfc <= x <= 0xfd : 6 bytes
2768 * 1111 111. 0xfe <= x <= 0xff : invalid sequence
2769 */
2770 switch (*p) {
2771 case 0x00 ... 0x7f:
2772 *c = *p;
2773 return UTF8_CODE_OK | 1;
2774
2775 case 0x80 ... 0xbf:
2776 *c = *p;
2777 return UTF8_CODE_BADSEQ | 1;
2778
2779 case 0xc0 ... 0xdf:
2780 if (len < 2) {
2781 *c = *p;
2782 return UTF8_CODE_BADSEQ | 1;
2783 }
2784 *c = *p & 0x1f;
2785 dec = 1;
2786 break;
2787
2788 case 0xe0 ... 0xef:
2789 if (len < 3) {
2790 *c = *p;
2791 return UTF8_CODE_BADSEQ | 1;
2792 }
2793 *c = *p & 0x0f;
2794 dec = 2;
2795 break;
2796
2797 case 0xf0 ... 0xf7:
2798 if (len < 4) {
2799 *c = *p;
2800 return UTF8_CODE_BADSEQ | 1;
2801 }
2802 *c = *p & 0x07;
2803 dec = 3;
2804 break;
2805
2806 case 0xf8 ... 0xfb:
2807 if (len < 5) {
2808 *c = *p;
2809 return UTF8_CODE_BADSEQ | 1;
2810 }
2811 *c = *p & 0x03;
2812 dec = 4;
2813 break;
2814
2815 case 0xfc ... 0xfd:
2816 if (len < 6) {
2817 *c = *p;
2818 return UTF8_CODE_BADSEQ | 1;
2819 }
2820 *c = *p & 0x01;
2821 dec = 5;
2822 break;
2823
2824 case 0xfe ... 0xff:
2825 default:
2826 *c = *p;
2827 return UTF8_CODE_BADSEQ | 1;
2828 }
2829
2830 p++;
2831
2832 while (dec > 0) {
2833
2834 /* need 0x10 for the 2 first bits */
2835 if ( ( *p & 0xc0 ) != 0x80 )
2836 return UTF8_CODE_BADSEQ | ((p-(unsigned char *)s)&0xffff);
2837
2838 /* add data at char */
2839 *c = ( *c << 6 ) | ( *p & 0x3f );
2840
2841 dec--;
2842 p++;
2843 }
2844
2845 /* Check ovelong encoding.
2846 * 1 byte : 5 + 6 : 11 : 0x80 ... 0x7ff
2847 * 2 bytes : 4 + 6 + 6 : 16 : 0x800 ... 0xffff
2848 * 3 bytes : 3 + 6 + 6 + 6 : 21 : 0x10000 ... 0x1fffff
2849 */
Thierry FOURNIER9e7ec082015-03-12 19:32:38 +01002850 if (( *c <= 0x7f && (p-(unsigned char *)s) > 1) ||
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002851 (*c >= 0x80 && *c <= 0x7ff && (p-(unsigned char *)s) > 2) ||
2852 (*c >= 0x800 && *c <= 0xffff && (p-(unsigned char *)s) > 3) ||
2853 (*c >= 0x10000 && *c <= 0x1fffff && (p-(unsigned char *)s) > 4))
2854 code |= UTF8_CODE_OVERLONG;
2855
2856 /* Check invalid UTF8 range. */
2857 if ((*c >= 0xd800 && *c <= 0xdfff) ||
2858 (*c >= 0xfffe && *c <= 0xffff))
2859 code |= UTF8_CODE_INVRANGE;
2860
2861 return code | ((p-(unsigned char *)s)&0x0f);
2862}
2863
Willy Tarreaubaaee002006-06-26 02:48:02 +02002864/*
2865 * Local variables:
2866 * c-indent-level: 8
2867 * c-basic-offset: 8
2868 * End:
2869 */