blob: 709db8b942e1aa3704a581eb4e82d74e6aa6c4e0 [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>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010028#include <eb32tree.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
Willy Tarreau56adcf22012-12-23 18:00:29 +010030/* enough to store NB_ITOA_STR integers of :
Willy Tarreau72d759c2007-10-25 12:14:10 +020031 * 2^64-1 = 18446744073709551615 or
32 * -2^63 = -9223372036854775808
Willy Tarreaue7239b52009-03-29 13:41:58 +020033 *
34 * The HTML version needs room for adding the 25 characters
35 * '<span class="rls"></span>' around digits at positions 3N+1 in order
36 * to add spacing at up to 6 positions : 18 446 744 073 709 551 615
Willy Tarreau72d759c2007-10-25 12:14:10 +020037 */
Willy Tarreau56adcf22012-12-23 18:00:29 +010038char itoa_str[NB_ITOA_STR][171];
39int itoa_idx = 0; /* index of next itoa_str to use */
Willy Tarreaubaaee002006-06-26 02:48:02 +020040
Willy Tarreau588297f2014-06-16 15:16:40 +020041/* sometimes we'll need to quote strings (eg: in stats), and we don't expect
42 * to quote strings larger than a max configuration line.
43 */
44char quoted_str[NB_QSTR][QSTR_SIZE + 1];
45int quoted_idx = 0;
46
Willy Tarreaubaaee002006-06-26 02:48:02 +020047/*
William Lallemande7340ec2012-01-24 11:15:39 +010048 * unsigned long long ASCII representation
49 *
50 * return the last char '\0' or NULL if no enough
51 * space in dst
52 */
53char *ulltoa(unsigned long long n, char *dst, size_t size)
54{
55 int i = 0;
56 char *res;
57
58 switch(n) {
59 case 1ULL ... 9ULL:
60 i = 0;
61 break;
62
63 case 10ULL ... 99ULL:
64 i = 1;
65 break;
66
67 case 100ULL ... 999ULL:
68 i = 2;
69 break;
70
71 case 1000ULL ... 9999ULL:
72 i = 3;
73 break;
74
75 case 10000ULL ... 99999ULL:
76 i = 4;
77 break;
78
79 case 100000ULL ... 999999ULL:
80 i = 5;
81 break;
82
83 case 1000000ULL ... 9999999ULL:
84 i = 6;
85 break;
86
87 case 10000000ULL ... 99999999ULL:
88 i = 7;
89 break;
90
91 case 100000000ULL ... 999999999ULL:
92 i = 8;
93 break;
94
95 case 1000000000ULL ... 9999999999ULL:
96 i = 9;
97 break;
98
99 case 10000000000ULL ... 99999999999ULL:
100 i = 10;
101 break;
102
103 case 100000000000ULL ... 999999999999ULL:
104 i = 11;
105 break;
106
107 case 1000000000000ULL ... 9999999999999ULL:
108 i = 12;
109 break;
110
111 case 10000000000000ULL ... 99999999999999ULL:
112 i = 13;
113 break;
114
115 case 100000000000000ULL ... 999999999999999ULL:
116 i = 14;
117 break;
118
119 case 1000000000000000ULL ... 9999999999999999ULL:
120 i = 15;
121 break;
122
123 case 10000000000000000ULL ... 99999999999999999ULL:
124 i = 16;
125 break;
126
127 case 100000000000000000ULL ... 999999999999999999ULL:
128 i = 17;
129 break;
130
131 case 1000000000000000000ULL ... 9999999999999999999ULL:
132 i = 18;
133 break;
134
135 case 10000000000000000000ULL ... ULLONG_MAX:
136 i = 19;
137 break;
138 }
139 if (i + 2 > size) // (i + 1) + '\0'
140 return NULL; // too long
141 res = dst + i + 1;
142 *res = '\0';
143 for (; i >= 0; i--) {
144 dst[i] = n % 10ULL + '0';
145 n /= 10ULL;
146 }
147 return res;
148}
149
150/*
151 * unsigned long ASCII representation
152 *
153 * return the last char '\0' or NULL if no enough
154 * space in dst
155 */
156char *ultoa_o(unsigned long n, char *dst, size_t size)
157{
158 int i = 0;
159 char *res;
160
161 switch (n) {
162 case 0U ... 9UL:
163 i = 0;
164 break;
165
166 case 10U ... 99UL:
167 i = 1;
168 break;
169
170 case 100U ... 999UL:
171 i = 2;
172 break;
173
174 case 1000U ... 9999UL:
175 i = 3;
176 break;
177
178 case 10000U ... 99999UL:
179 i = 4;
180 break;
181
182 case 100000U ... 999999UL:
183 i = 5;
184 break;
185
186 case 1000000U ... 9999999UL:
187 i = 6;
188 break;
189
190 case 10000000U ... 99999999UL:
191 i = 7;
192 break;
193
194 case 100000000U ... 999999999UL:
195 i = 8;
196 break;
197#if __WORDSIZE == 32
198
199 case 1000000000ULL ... ULONG_MAX:
200 i = 9;
201 break;
202
203#elif __WORDSIZE == 64
204
205 case 1000000000ULL ... 9999999999UL:
206 i = 9;
207 break;
208
209 case 10000000000ULL ... 99999999999UL:
210 i = 10;
211 break;
212
213 case 100000000000ULL ... 999999999999UL:
214 i = 11;
215 break;
216
217 case 1000000000000ULL ... 9999999999999UL:
218 i = 12;
219 break;
220
221 case 10000000000000ULL ... 99999999999999UL:
222 i = 13;
223 break;
224
225 case 100000000000000ULL ... 999999999999999UL:
226 i = 14;
227 break;
228
229 case 1000000000000000ULL ... 9999999999999999UL:
230 i = 15;
231 break;
232
233 case 10000000000000000ULL ... 99999999999999999UL:
234 i = 16;
235 break;
236
237 case 100000000000000000ULL ... 999999999999999999UL:
238 i = 17;
239 break;
240
241 case 1000000000000000000ULL ... 9999999999999999999UL:
242 i = 18;
243 break;
244
245 case 10000000000000000000ULL ... ULONG_MAX:
246 i = 19;
247 break;
248
249#endif
250 }
251 if (i + 2 > size) // (i + 1) + '\0'
252 return NULL; // too long
253 res = dst + i + 1;
254 *res = '\0';
255 for (; i >= 0; i--) {
256 dst[i] = n % 10U + '0';
257 n /= 10U;
258 }
259 return res;
260}
261
262/*
263 * signed long ASCII representation
264 *
265 * return the last char '\0' or NULL if no enough
266 * space in dst
267 */
268char *ltoa_o(long int n, char *dst, size_t size)
269{
270 char *pos = dst;
271
272 if (n < 0) {
273 if (size < 3)
274 return NULL; // min size is '-' + digit + '\0' but another test in ultoa
275 *pos = '-';
276 pos++;
277 dst = ultoa_o(-n, pos, size - 1);
278 } else {
279 dst = ultoa_o(n, dst, size);
280 }
281 return dst;
282}
283
284/*
285 * signed long long ASCII representation
286 *
287 * return the last char '\0' or NULL if no enough
288 * space in dst
289 */
290char *lltoa(long long n, char *dst, size_t size)
291{
292 char *pos = dst;
293
294 if (n < 0) {
295 if (size < 3)
296 return NULL; // min size is '-' + digit + '\0' but another test in ulltoa
297 *pos = '-';
298 pos++;
299 dst = ulltoa(-n, pos, size - 1);
300 } else {
301 dst = ulltoa(n, dst, size);
302 }
303 return dst;
304}
305
306/*
307 * write a ascii representation of a unsigned into dst,
308 * return a pointer to the last character
309 * Pad the ascii representation with '0', using size.
310 */
311char *utoa_pad(unsigned int n, char *dst, size_t size)
312{
313 int i = 0;
314 char *ret;
315
316 switch(n) {
317 case 0U ... 9U:
318 i = 0;
319 break;
320
321 case 10U ... 99U:
322 i = 1;
323 break;
324
325 case 100U ... 999U:
326 i = 2;
327 break;
328
329 case 1000U ... 9999U:
330 i = 3;
331 break;
332
333 case 10000U ... 99999U:
334 i = 4;
335 break;
336
337 case 100000U ... 999999U:
338 i = 5;
339 break;
340
341 case 1000000U ... 9999999U:
342 i = 6;
343 break;
344
345 case 10000000U ... 99999999U:
346 i = 7;
347 break;
348
349 case 100000000U ... 999999999U:
350 i = 8;
351 break;
352
353 case 1000000000U ... 4294967295U:
354 i = 9;
355 break;
356 }
357 if (i + 2 > size) // (i + 1) + '\0'
358 return NULL; // too long
359 if (i < size)
360 i = size - 2; // padding - '\0'
361
362 ret = dst + i + 1;
363 *ret = '\0';
364 for (; i >= 0; i--) {
365 dst[i] = n % 10U + '0';
366 n /= 10U;
367 }
368 return ret;
369}
370
371/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200372 * copies at most <size-1> chars from <src> to <dst>. Last char is always
373 * set to 0, unless <size> is 0. The number of chars copied is returned
374 * (excluding the terminating zero).
375 * This code has been optimized for size and speed : on x86, it's 45 bytes
376 * long, uses only registers, and consumes only 4 cycles per char.
377 */
378int strlcpy2(char *dst, const char *src, int size)
379{
380 char *orig = dst;
381 if (size) {
382 while (--size && (*dst = *src)) {
383 src++; dst++;
384 }
385 *dst = 0;
386 }
387 return dst - orig;
388}
389
390/*
Willy Tarreau72d759c2007-10-25 12:14:10 +0200391 * This function simply returns a locally allocated string containing
Willy Tarreaubaaee002006-06-26 02:48:02 +0200392 * the ascii representation for number 'n' in decimal.
393 */
Emeric Brun3a7fce52010-01-04 14:54:38 +0100394char *ultoa_r(unsigned long n, char *buffer, int size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200395{
396 char *pos;
397
Willy Tarreau72d759c2007-10-25 12:14:10 +0200398 pos = buffer + size - 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399 *pos-- = '\0';
400
401 do {
402 *pos-- = '0' + n % 10;
403 n /= 10;
Willy Tarreau72d759c2007-10-25 12:14:10 +0200404 } while (n && pos >= buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200405 return pos + 1;
406}
407
Willy Tarreau91092e52007-10-25 16:58:42 +0200408/*
Willy Tarreaue7239b52009-03-29 13:41:58 +0200409 * This function simply returns a locally allocated string containing
410 * the ascii representation for number 'n' in decimal, formatted for
411 * HTML output with tags to create visual grouping by 3 digits. The
412 * output needs to support at least 171 characters.
413 */
414const char *ulltoh_r(unsigned long long n, char *buffer, int size)
415{
416 char *start;
417 int digit = 0;
418
419 start = buffer + size;
420 *--start = '\0';
421
422 do {
423 if (digit == 3 && start >= buffer + 7)
424 memcpy(start -= 7, "</span>", 7);
425
426 if (start >= buffer + 1) {
427 *--start = '0' + n % 10;
428 n /= 10;
429 }
430
431 if (digit == 3 && start >= buffer + 18)
432 memcpy(start -= 18, "<span class=\"rls\">", 18);
433
434 if (digit++ == 3)
435 digit = 1;
436 } while (n && start > buffer);
437 return start;
438}
439
440/*
Willy Tarreau91092e52007-10-25 16:58:42 +0200441 * This function simply returns a locally allocated string containing the ascii
442 * representation for number 'n' in decimal, unless n is 0 in which case it
443 * returns the alternate string (or an empty string if the alternate string is
444 * NULL). It use is intended for limits reported in reports, where it's
445 * desirable not to display anything if there is no limit. Warning! it shares
446 * the same vector as ultoa_r().
447 */
448const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
449{
450 return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
451}
452
Willy Tarreau588297f2014-06-16 15:16:40 +0200453/* returns a locally allocated string containing the quoted encoding of the
454 * input string. The output may be truncated to QSTR_SIZE chars, but it is
455 * guaranteed that the string will always be properly terminated. Quotes are
456 * encoded by doubling them as is commonly done in CSV files. QSTR_SIZE must
457 * always be at least 4 chars.
458 */
459const char *qstr(const char *str)
460{
461 char *ret = quoted_str[quoted_idx];
462 char *p, *end;
463
464 if (++quoted_idx >= NB_QSTR)
465 quoted_idx = 0;
466
467 p = ret;
468 end = ret + QSTR_SIZE;
469
470 *p++ = '"';
471
472 /* always keep 3 chars to support passing "" and the ending " */
473 while (*str && p < end - 3) {
474 if (*str == '"') {
475 *p++ = '"';
476 *p++ = '"';
477 }
478 else
479 *p++ = *str;
480 str++;
481 }
482 *p++ = '"';
483 return ret;
484}
485
Robert Tsai81ae1952007-12-05 10:47:29 +0100486/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200487 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
488 *
489 * It looks like this one would be a good candidate for inlining, but this is
490 * not interesting because it around 35 bytes long and often called multiple
491 * times within the same function.
492 */
493int ishex(char s)
494{
495 s -= '0';
496 if ((unsigned char)s <= 9)
497 return 1;
498 s -= 'A' - '0';
499 if ((unsigned char)s <= 5)
500 return 1;
501 s -= 'a' - 'A';
502 if ((unsigned char)s <= 5)
503 return 1;
504 return 0;
505}
506
Willy Tarreau3ca1a882015-01-15 18:43:49 +0100507/* rounds <i> down to the closest value having max 2 digits */
508unsigned int round_2dig(unsigned int i)
509{
510 unsigned int mul = 1;
511
512 while (i >= 100) {
513 i /= 10;
514 mul *= 10;
515 }
516 return i * mul;
517}
518
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100519/*
520 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
521 * invalid character is found, a pointer to it is returned. If everything is
522 * fine, NULL is returned.
523 */
524const char *invalid_char(const char *name)
525{
526 if (!*name)
527 return name;
528
529 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100530 if (!isalnum((int)(unsigned char)*name) && *name != '.' && *name != ':' &&
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100531 *name != '_' && *name != '-')
532 return name;
533 name++;
534 }
535 return NULL;
536}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200537
538/*
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200539 * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-].
540 * If an invalid character is found, a pointer to it is returned.
541 * If everything is fine, NULL is returned.
542 */
543const char *invalid_domainchar(const char *name) {
544
545 if (!*name)
546 return name;
547
548 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100549 if (!isalnum((int)(unsigned char)*name) && *name != '.' &&
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200550 *name != '_' && *name != '-')
551 return name;
552
553 name++;
554 }
555
556 return NULL;
557}
558
559/*
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100560 * converts <str> to a struct sockaddr_storage* provided by the caller. The
Willy Tarreau24709282013-03-10 21:32:12 +0100561 * caller must have zeroed <sa> first, and may have set sa->ss_family to force
562 * parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then
563 * the function tries to guess the address family from the syntax. If the
564 * family is forced and the format doesn't match, an error is returned. The
Willy Tarreaufab5a432011-03-04 15:31:53 +0100565 * string is assumed to contain only an address, no port. The address can be a
566 * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to
567 * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved.
568 * The return address will only have the address family and the address set,
569 * all other fields remain zero. The string is not supposed to be modified.
Thierry FOURNIER58639a02014-11-25 12:02:25 +0100570 * The IPv6 '::' address is IN6ADDR_ANY. If <resolve> is non-zero, the hostname
571 * is resolved, otherwise only IP addresses are resolved, and anything else
572 * returns NULL.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573 */
Thierry FOURNIER58639a02014-11-25 12:02:25 +0100574struct sockaddr_storage *str2ip2(const char *str, struct sockaddr_storage *sa, int resolve)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200575{
Willy Tarreaufab5a432011-03-04 15:31:53 +0100576 struct hostent *he;
577
Willy Tarreaufab5a432011-03-04 15:31:53 +0100578 /* Any IPv6 address */
579 if (str[0] == ':' && str[1] == ':' && !str[2]) {
Willy Tarreau24709282013-03-10 21:32:12 +0100580 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
581 sa->ss_family = AF_INET6;
582 else if (sa->ss_family != AF_INET6)
583 goto fail;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100584 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100585 }
586
Willy Tarreau24709282013-03-10 21:32:12 +0100587 /* Any address for the family, defaults to IPv4 */
Willy Tarreaufab5a432011-03-04 15:31:53 +0100588 if (!str[0] || (str[0] == '*' && !str[1])) {
Willy Tarreau24709282013-03-10 21:32:12 +0100589 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
590 sa->ss_family = AF_INET;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100591 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100592 }
593
594 /* check for IPv6 first */
Willy Tarreau24709282013-03-10 21:32:12 +0100595 if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET6) &&
596 inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)sa)->sin6_addr)) {
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100597 sa->ss_family = AF_INET6;
598 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100599 }
600
601 /* then check for IPv4 */
Willy Tarreau24709282013-03-10 21:32:12 +0100602 if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET) &&
603 inet_pton(AF_INET, str, &((struct sockaddr_in *)sa)->sin_addr)) {
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100604 sa->ss_family = AF_INET;
605 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100606 }
607
Thierry FOURNIER58639a02014-11-25 12:02:25 +0100608 if (!resolve)
609 return NULL;
610
David du Colombierd5f43282011-03-17 10:40:16 +0100611#ifdef USE_GETADDRINFO
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200612 if (global.tune.options & GTUNE_USE_GAI) {
David du Colombierd5f43282011-03-17 10:40:16 +0100613 struct addrinfo hints, *result;
614
615 memset(&result, 0, sizeof(result));
616 memset(&hints, 0, sizeof(hints));
Willy Tarreau24709282013-03-10 21:32:12 +0100617 hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC;
David du Colombierd5f43282011-03-17 10:40:16 +0100618 hints.ai_socktype = SOCK_DGRAM;
619 hints.ai_flags = AI_PASSIVE;
620 hints.ai_protocol = 0;
621
622 if (getaddrinfo(str, NULL, &hints, &result) == 0) {
Willy Tarreau24709282013-03-10 21:32:12 +0100623 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
624 sa->ss_family = result->ai_family;
625 else if (sa->ss_family != result->ai_family)
626 goto fail;
627
David du Colombierd5f43282011-03-17 10:40:16 +0100628 switch (result->ai_family) {
629 case AF_INET:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100630 memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen);
631 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100632 case AF_INET6:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100633 memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen);
634 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100635 }
636 }
637
Sean Carey58ea0392013-02-15 23:39:18 +0100638 if (result)
639 freeaddrinfo(result);
Willy Tarreaufab5a432011-03-04 15:31:53 +0100640 }
David du Colombierd5f43282011-03-17 10:40:16 +0100641#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200642 /* try to resolve an IPv4/IPv6 hostname */
643 he = gethostbyname(str);
644 if (he) {
645 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
646 sa->ss_family = he->h_addrtype;
647 else if (sa->ss_family != he->h_addrtype)
648 goto fail;
649
650 switch (sa->ss_family) {
651 case AF_INET:
652 ((struct sockaddr_in *)sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
653 return sa;
654 case AF_INET6:
655 ((struct sockaddr_in6 *)sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
656 return sa;
657 }
658 }
659
David du Colombierd5f43282011-03-17 10:40:16 +0100660 /* unsupported address family */
Willy Tarreau24709282013-03-10 21:32:12 +0100661 fail:
Willy Tarreaufab5a432011-03-04 15:31:53 +0100662 return NULL;
663}
664
665/*
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100666 * Converts <str> to a locally allocated struct sockaddr_storage *, and a port
667 * range or offset consisting in two integers that the caller will have to
668 * check to find the relevant input format. The following format are supported :
669 *
670 * String format | address | port | low | high
671 * addr | <addr> | 0 | 0 | 0
672 * addr: | <addr> | 0 | 0 | 0
673 * addr:port | <addr> | <port> | <port> | <port>
674 * addr:pl-ph | <addr> | <pl> | <pl> | <ph>
675 * addr:+port | <addr> | <port> | 0 | <port>
676 * addr:-port | <addr> |-<port> | <port> | 0
677 *
678 * The detection of a port range or increment by the caller is made by
679 * comparing <low> and <high>. If both are equal, then port 0 means no port
680 * was specified. The caller may pass NULL for <low> and <high> if it is not
681 * interested in retrieving port ranges.
682 *
683 * Note that <addr> above may also be :
684 * - empty ("") => family will be AF_INET and address will be INADDR_ANY
685 * - "*" => family will be AF_INET and address will be INADDR_ANY
686 * - "::" => family will be AF_INET6 and address will be IN6ADDR_ANY
687 * - a host name => family and address will depend on host name resolving.
688 *
Willy Tarreau24709282013-03-10 21:32:12 +0100689 * A prefix may be passed in before the address above to force the family :
690 * - "ipv4@" => force address to resolve as IPv4 and fail if not possible.
691 * - "ipv6@" => force address to resolve as IPv6 and fail if not possible.
692 * - "unix@" => force address to be a path to a UNIX socket even if the
693 * path does not start with a '/'
Willy Tarreauccfccef2014-05-10 01:49:15 +0200694 * - 'abns@' -> force address to belong to the abstract namespace (Linux
695 * only). These sockets are just like Unix sockets but without
696 * the need for an underlying file system. The address is a
697 * string. Technically it's like a Unix socket with a zero in
698 * the first byte of the address.
Willy Tarreau40aa0702013-03-10 23:51:38 +0100699 * - "fd@" => an integer must follow, and is a file descriptor number.
Willy Tarreau24709282013-03-10 21:32:12 +0100700 *
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100701 * Also note that in order to avoid any ambiguity with IPv6 addresses, the ':'
702 * is mandatory after the IP address even when no port is specified. NULL is
703 * returned if the address cannot be parsed. The <low> and <high> ports are
Willy Tarreau24709282013-03-10 21:32:12 +0100704 * always initialized if non-null, even for non-IP families.
Willy Tarreaud393a622013-03-04 18:22:00 +0100705 *
706 * If <pfx> is non-null, it is used as a string prefix before any path-based
707 * address (typically the path to a unix socket).
Willy Tarreau40aa0702013-03-10 23:51:38 +0100708 *
709 * When a file descriptor is passed, its value is put into the s_addr part of
710 * the address when cast to sockaddr_in and the address family is AF_UNSPEC.
Willy Tarreaufab5a432011-03-04 15:31:53 +0100711 */
Willy Tarreaud393a622013-03-04 18:22:00 +0100712struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high, char **err, const char *pfx)
Willy Tarreaufab5a432011-03-04 15:31:53 +0100713{
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100714 static struct sockaddr_storage ss;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100715 struct sockaddr_storage *ret = NULL;
Willy Tarreau24709282013-03-10 21:32:12 +0100716 char *back, *str2;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100717 char *port1, *port2;
718 int portl, porth, porta;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200719 int abstract = 0;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100720
721 portl = porth = porta = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200722
Willy Tarreaudad36a32013-03-11 01:20:04 +0100723 str2 = back = env_expand(strdup(str));
Willy Tarreaudf350f12013-03-01 20:22:54 +0100724 if (str2 == NULL) {
725 memprintf(err, "out of memory in '%s'\n", __FUNCTION__);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100726 goto out;
Willy Tarreaudf350f12013-03-01 20:22:54 +0100727 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200728
Willy Tarreau24709282013-03-10 21:32:12 +0100729 memset(&ss, 0, sizeof(ss));
730
731 if (strncmp(str2, "unix@", 5) == 0) {
732 str2 += 5;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200733 abstract = 0;
Willy Tarreau24709282013-03-10 21:32:12 +0100734 ss.ss_family = AF_UNIX;
735 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200736 else if (strncmp(str2, "abns@", 5) == 0) {
737 str2 += 5;
738 abstract = 1;
739 ss.ss_family = AF_UNIX;
740 }
Willy Tarreau24709282013-03-10 21:32:12 +0100741 else if (strncmp(str2, "ipv4@", 5) == 0) {
742 str2 += 5;
743 ss.ss_family = AF_INET;
744 }
745 else if (strncmp(str2, "ipv6@", 5) == 0) {
746 str2 += 5;
747 ss.ss_family = AF_INET6;
748 }
749 else if (*str2 == '/') {
750 ss.ss_family = AF_UNIX;
751 }
752 else
753 ss.ss_family = AF_UNSPEC;
754
Willy Tarreau40aa0702013-03-10 23:51:38 +0100755 if (ss.ss_family == AF_UNSPEC && strncmp(str2, "fd@", 3) == 0) {
756 char *endptr;
757
758 str2 += 3;
759 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = strtol(str2, &endptr, 10);
760
761 if (!*str2 || *endptr) {
Willy Tarreaudad36a32013-03-11 01:20:04 +0100762 memprintf(err, "file descriptor '%s' is not a valid integer in '%s'\n", str2, str);
Willy Tarreau40aa0702013-03-10 23:51:38 +0100763 goto out;
764 }
765
766 /* we return AF_UNSPEC if we use a file descriptor number */
767 ss.ss_family = AF_UNSPEC;
768 }
769 else if (ss.ss_family == AF_UNIX) {
Willy Tarreau15586382013-03-04 19:48:14 +0100770 int prefix_path_len;
771 int max_path_len;
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200772 int adr_len;
Willy Tarreau15586382013-03-04 19:48:14 +0100773
774 /* complete unix socket path name during startup or soft-restart is
775 * <unix_bind_prefix><path>.<pid>.<bak|tmp>
776 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200777 prefix_path_len = (pfx && !abstract) ? strlen(pfx) : 0;
Willy Tarreau15586382013-03-04 19:48:14 +0100778 max_path_len = (sizeof(((struct sockaddr_un *)&ss)->sun_path) - 1) -
779 (prefix_path_len ? prefix_path_len + 1 + 5 + 1 + 3 : 0);
780
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200781 adr_len = strlen(str2);
782 if (adr_len > max_path_len) {
Willy Tarreau15586382013-03-04 19:48:14 +0100783 memprintf(err, "socket path '%s' too long (max %d)\n", str, max_path_len);
784 goto out;
785 }
786
Willy Tarreauccfccef2014-05-10 01:49:15 +0200787 /* when abstract==1, we skip the first zero and copy all bytes except the trailing zero */
788 memset(((struct sockaddr_un *)&ss)->sun_path, 0, sizeof(((struct sockaddr_un *)&ss)->sun_path));
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200789 if (prefix_path_len)
Willy Tarreau15586382013-03-04 19:48:14 +0100790 memcpy(((struct sockaddr_un *)&ss)->sun_path, pfx, prefix_path_len);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200791 memcpy(((struct sockaddr_un *)&ss)->sun_path + prefix_path_len + abstract, str2, adr_len + 1 - abstract);
Willy Tarreau15586382013-03-04 19:48:14 +0100792 }
Willy Tarreau24709282013-03-10 21:32:12 +0100793 else { /* IPv4 and IPv6 */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100794 port1 = strrchr(str2, ':');
795 if (port1)
796 *port1++ = '\0';
797 else
798 port1 = "";
Willy Tarreaubaaee002006-06-26 02:48:02 +0200799
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100800 if (str2ip(str2, &ss) == NULL) {
801 memprintf(err, "invalid address: '%s' in '%s'\n", str2, str);
802 goto out;
803 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100804
Willy Tarreaua39d1992013-04-01 20:37:42 +0200805 if (isdigit((int)(unsigned char)*port1)) { /* single port or range */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100806 port2 = strchr(port1, '-');
807 if (port2)
808 *port2++ = '\0';
809 else
810 port2 = port1;
811 portl = atoi(port1);
812 porth = atoi(port2);
813 porta = portl;
814 }
815 else if (*port1 == '-') { /* negative offset */
816 portl = atoi(port1 + 1);
817 porta = -portl;
818 }
819 else if (*port1 == '+') { /* positive offset */
820 porth = atoi(port1 + 1);
821 porta = porth;
822 }
823 else if (*port1) { /* other any unexpected char */
Willy Tarreaudad36a32013-03-11 01:20:04 +0100824 memprintf(err, "invalid character '%c' in port number '%s' in '%s'\n", *port1, port1, str);
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100825 goto out;
826 }
827 set_host_port(&ss, porta);
Willy Tarreaue4c58c82013-03-06 15:28:17 +0100828 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100829
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100830 ret = &ss;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100831 out:
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100832 if (low)
833 *low = portl;
834 if (high)
835 *high = porth;
Willy Tarreau24709282013-03-10 21:32:12 +0100836 free(back);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100837 return ret;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200838}
839
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100840/* converts <str> to a struct in_addr containing a network mask. It can be
841 * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1
842 * if the conversion succeeds otherwise non-zero.
843 */
844int str2mask(const char *str, struct in_addr *mask)
845{
846 if (strchr(str, '.') != NULL) { /* dotted notation */
847 if (!inet_pton(AF_INET, str, mask))
848 return 0;
849 }
850 else { /* mask length */
851 char *err;
852 unsigned long len = strtol(str, &err, 10);
853
854 if (!*str || (err && *err) || (unsigned)len > 32)
855 return 0;
856 if (len)
857 mask->s_addr = htonl(~0UL << (32 - len));
858 else
859 mask->s_addr = 0;
860 }
861 return 1;
862}
863
Thierry FOURNIERb0504632013-12-14 15:39:02 +0100864/* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
865 * succeeds otherwise zero.
866 */
867int cidr2dotted(int cidr, struct in_addr *mask) {
868
869 if (cidr < 0 || cidr > 32)
870 return 0;
871
872 mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0;
873 return 1;
874}
875
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200876/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200877 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200878 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
879 * is optionnal and either in the dotted or CIDR notation.
880 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
881 */
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100882int str2net(const char *str, int resolve, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200883{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200884 __label__ out_free, out_err;
885 char *c, *s;
886 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200887
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200888 s = strdup(str);
889 if (!s)
890 return 0;
891
Willy Tarreaubaaee002006-06-26 02:48:02 +0200892 memset(mask, 0, sizeof(*mask));
893 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200894
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200895 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200896 *c++ = '\0';
897 /* c points to the mask */
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100898 if (!str2mask(c, mask))
899 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200900 }
901 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100902 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200903 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200904 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200905 struct hostent *he;
906
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100907 if (!resolve)
908 goto out_err;
909
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200910 if ((he = gethostbyname(s)) == NULL) {
911 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200912 }
913 else
914 *addr = *(struct in_addr *) *(he->h_addr_list);
915 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200916
917 ret_val = 1;
918 out_free:
919 free(s);
920 return ret_val;
921 out_err:
922 ret_val = 0;
923 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200924}
925
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100926
927/*
Willy Tarreau6d20e282012-04-27 22:49:47 +0200928 * converts <str> to two struct in6_addr* which must be pre-allocated.
929 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
930 * is an optionnal number of bits (128 being the default).
931 * Returns 1 if OK, 0 if error.
932 */
933int str62net(const char *str, struct in6_addr *addr, unsigned char *mask)
934{
935 char *c, *s;
936 int ret_val = 0;
937 char *err;
938 unsigned long len = 128;
939
940 s = strdup(str);
941 if (!s)
942 return 0;
943
944 memset(mask, 0, sizeof(*mask));
945 memset(addr, 0, sizeof(*addr));
946
947 if ((c = strrchr(s, '/')) != NULL) {
948 *c++ = '\0'; /* c points to the mask */
949 if (!*c)
950 goto out_free;
951
952 len = strtoul(c, &err, 10);
953 if ((err && *err) || (unsigned)len > 128)
954 goto out_free;
955 }
956 *mask = len; /* OK we have a valid mask in <len> */
957
958 if (!inet_pton(AF_INET6, s, addr))
959 goto out_free;
960
961 ret_val = 1;
962 out_free:
963 free(s);
964 return ret_val;
965}
966
967
968/*
David du Colombier6f5ccb12011-03-10 22:26:24 +0100969 * Parse IPv4 address found in url.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100970 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100971int url2ipv4(const char *addr, struct in_addr *dst)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100972{
973 int saw_digit, octets, ch;
974 u_char tmp[4], *tp;
975 const char *cp = addr;
976
977 saw_digit = 0;
978 octets = 0;
979 *(tp = tmp) = 0;
980
981 while (*addr) {
982 unsigned char digit = (ch = *addr++) - '0';
983 if (digit > 9 && ch != '.')
984 break;
985 if (digit <= 9) {
986 u_int new = *tp * 10 + digit;
987 if (new > 255)
988 return 0;
989 *tp = new;
990 if (!saw_digit) {
991 if (++octets > 4)
992 return 0;
993 saw_digit = 1;
994 }
995 } else if (ch == '.' && saw_digit) {
996 if (octets == 4)
997 return 0;
998 *++tp = 0;
999 saw_digit = 0;
1000 } else
1001 return 0;
1002 }
1003
1004 if (octets < 4)
1005 return 0;
1006
1007 memcpy(&dst->s_addr, tmp, 4);
1008 return addr-cp-1;
1009}
1010
1011/*
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001012 * Resolve destination server from URL. Convert <str> to a sockaddr_storage.
1013 * <out> contain the code of the dectected scheme, the start and length of
1014 * the hostname. Actually only http and https are supported. <out> can be NULL.
1015 * This function returns the consumed length. It is useful if you parse complete
1016 * url like http://host:port/path, because the consumed length corresponds to
1017 * the first character of the path. If the conversion fails, it returns -1.
1018 *
1019 * This function tries to resolve the DNS name if haproxy is in starting mode.
1020 * So, this function may be used during the configuration parsing.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001021 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001022int url2sa(const char *url, int ulen, struct sockaddr_storage *addr, struct split_url *out)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001023{
1024 const char *curr = url, *cp = url;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001025 const char *end;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001026 int ret, url_code = 0;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001027 unsigned long long int http_code = 0;
1028 int default_port;
1029 struct hostent *he;
1030 char *p;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001031
1032 /* Firstly, try to find :// pattern */
1033 while (curr < url+ulen && url_code != 0x3a2f2f) {
1034 url_code = ((url_code & 0xffff) << 8);
1035 url_code += (unsigned char)*curr++;
1036 }
1037
1038 /* Secondly, if :// pattern is found, verify parsed stuff
1039 * before pattern is matching our http pattern.
1040 * If so parse ip address and port in uri.
1041 *
1042 * WARNING: Current code doesn't support dynamic async dns resolver.
1043 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001044 if (url_code != 0x3a2f2f)
1045 return -1;
1046
1047 /* Copy scheme, and utrn to lower case. */
1048 while (cp < curr - 3)
1049 http_code = (http_code << 8) + *cp++;
1050 http_code |= 0x2020202020202020ULL; /* Turn everything to lower case */
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001051
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001052 /* HTTP or HTTPS url matching */
1053 if (http_code == 0x2020202068747470ULL) {
1054 default_port = 80;
1055 if (out)
1056 out->scheme = SCH_HTTP;
1057 }
1058 else if (http_code == 0x2020206874747073ULL) {
1059 default_port = 443;
1060 if (out)
1061 out->scheme = SCH_HTTPS;
1062 }
1063 else
1064 return -1;
1065
1066 /* If the next char is '[', the host address is IPv6. */
1067 if (*curr == '[') {
1068 curr++;
1069
1070 /* Check trash size */
1071 if (trash.size < ulen)
1072 return -1;
1073
1074 /* Look for ']' and copy the address in a trash buffer. */
1075 p = trash.str;
1076 for (end = curr;
1077 end < url + ulen && *end != ']';
1078 end++, p++)
1079 *p = *end;
1080 if (*end != ']')
1081 return -1;
1082 *p = '\0';
1083
1084 /* Update out. */
1085 if (out) {
1086 out->host = curr;
1087 out->host_len = end - curr;
1088 }
1089
1090 /* Try IPv6 decoding. */
1091 if (!inet_pton(AF_INET6, trash.str, &((struct sockaddr_in6 *)addr)->sin6_addr))
1092 return -1;
1093 end++;
1094
1095 /* Decode port. */
1096 if (*end == ':') {
1097 end++;
1098 default_port = read_uint(&end, url + ulen);
1099 }
1100 ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port);
1101 ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
1102 return end - url;
1103 }
1104 else {
1105 /* We are looking for IP address. If you want to parse and
1106 * resolve hostname found in url, you can use str2sa_range(), but
1107 * be warned this can slow down global daemon performances
1108 * while handling lagging dns responses.
1109 */
1110 ret = url2ipv4(curr, &((struct sockaddr_in *)addr)->sin_addr);
1111 if (ret) {
1112 /* Update out. */
1113 if (out) {
1114 out->host = curr;
1115 out->host_len = ret;
1116 }
1117
1118 curr += ret;
1119
1120 /* Decode port. */
1121 if (*curr == ':') {
1122 curr++;
1123 default_port = read_uint(&curr, url + ulen);
1124 }
1125 ((struct sockaddr_in *)addr)->sin_port = htons(default_port);
1126
1127 /* Set family. */
1128 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
1129 return curr - url;
1130 }
1131 else if (global.mode & MODE_STARTING) {
1132 /* The IPv4 and IPv6 decoding fails, maybe the url contain name. Try to execute
1133 * synchronous DNS request only if HAProxy is in the start state.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001134 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001135
1136 /* look for : or / or end */
1137 for (end = curr;
1138 end < url + ulen && *end != '/' && *end != ':';
1139 end++);
1140 memcpy(trash.str, curr, end - curr);
1141 trash.str[end - curr] = '\0';
1142
1143 /* try to resolve an IPv4/IPv6 hostname */
1144 he = gethostbyname(trash.str);
1145 if (!he)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001146 return -1;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001147
1148 /* Update out. */
1149 if (out) {
1150 out->host = curr;
1151 out->host_len = end - curr;
1152 }
1153
1154 /* Decode port. */
1155 if (*end == ':') {
1156 end++;
1157 default_port = read_uint(&end, url + ulen);
1158 }
1159
1160 /* Copy IP address, set port and family. */
1161 switch (he->h_addrtype) {
1162 case AF_INET:
1163 ((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
1164 ((struct sockaddr_in *)addr)->sin_port = htons(default_port);
1165 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
1166 return end - url;
1167
1168 case AF_INET6:
1169 ((struct sockaddr_in6 *)addr)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
1170 ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port);
1171 ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
1172 return end - url;
1173 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001174 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001175 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001176 return -1;
1177}
1178
Willy Tarreau631f01c2011-09-05 00:36:48 +02001179/* Tries to convert a sockaddr_storage address to text form. Upon success, the
1180 * address family is returned so that it's easy for the caller to adapt to the
1181 * output format. Zero is returned if the address family is not supported. -1
1182 * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
1183 * supported.
1184 */
1185int addr_to_str(struct sockaddr_storage *addr, char *str, int size)
1186{
1187
1188 void *ptr;
1189
1190 if (size < 5)
1191 return 0;
1192 *str = '\0';
1193
1194 switch (addr->ss_family) {
1195 case AF_INET:
1196 ptr = &((struct sockaddr_in *)addr)->sin_addr;
1197 break;
1198 case AF_INET6:
1199 ptr = &((struct sockaddr_in6 *)addr)->sin6_addr;
1200 break;
1201 case AF_UNIX:
1202 memcpy(str, "unix", 5);
1203 return addr->ss_family;
1204 default:
1205 return 0;
1206 }
1207
1208 if (inet_ntop(addr->ss_family, ptr, str, size))
1209 return addr->ss_family;
1210
1211 /* failed */
1212 return -1;
1213}
1214
Simon Horman75ab8bd2014-06-16 09:39:41 +09001215/* Tries to convert a sockaddr_storage port to text form. Upon success, the
1216 * address family is returned so that it's easy for the caller to adapt to the
1217 * output format. Zero is returned if the address family is not supported. -1
1218 * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
1219 * supported.
1220 */
1221int port_to_str(struct sockaddr_storage *addr, char *str, int size)
1222{
1223
1224 uint16_t port;
1225
1226
1227 if (size < 5)
1228 return 0;
1229 *str = '\0';
1230
1231 switch (addr->ss_family) {
1232 case AF_INET:
1233 port = ((struct sockaddr_in *)addr)->sin_port;
1234 break;
1235 case AF_INET6:
1236 port = ((struct sockaddr_in6 *)addr)->sin6_port;
1237 break;
1238 case AF_UNIX:
1239 memcpy(str, "unix", 5);
1240 return addr->ss_family;
1241 default:
1242 return 0;
1243 }
1244
1245 snprintf(str, size, "%u", ntohs(port));
1246 return addr->ss_family;
1247}
1248
Willy Tarreaubaaee002006-06-26 02:48:02 +02001249/* will try to encode the string <string> replacing all characters tagged in
1250 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
1251 * prefixed by <escape>, and will store the result between <start> (included)
1252 * and <stop> (excluded), and will always terminate the string with a '\0'
1253 * before <stop>. The position of the '\0' is returned if the conversion
1254 * completes. If bytes are missing between <start> and <stop>, then the
1255 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
1256 * cannot even be stored so we return <start> without writing the 0.
1257 * The input string must also be zero-terminated.
1258 */
1259const char hextab[16] = "0123456789ABCDEF";
1260char *encode_string(char *start, char *stop,
1261 const char escape, const fd_set *map,
1262 const char *string)
1263{
1264 if (start < stop) {
1265 stop--; /* reserve one byte for the final '\0' */
1266 while (start < stop && *string != '\0') {
1267 if (!FD_ISSET((unsigned char)(*string), map))
1268 *start++ = *string;
1269 else {
1270 if (start + 3 >= stop)
1271 break;
1272 *start++ = escape;
1273 *start++ = hextab[(*string >> 4) & 15];
1274 *start++ = hextab[*string & 15];
1275 }
1276 string++;
1277 }
1278 *start = '\0';
1279 }
1280 return start;
1281}
1282
Thierry FOURNIERe059ec92014-03-17 12:01:13 +01001283/*
1284 * Same behavior as encode_string() above, except that it encodes chunk
1285 * <chunk> instead of a string.
1286 */
1287char *encode_chunk(char *start, char *stop,
1288 const char escape, const fd_set *map,
1289 const struct chunk *chunk)
1290{
1291 char *str = chunk->str;
1292 char *end = chunk->str + chunk->len;
1293
1294 if (start < stop) {
1295 stop--; /* reserve one byte for the final '\0' */
1296 while (start < stop && str < end) {
1297 if (!FD_ISSET((unsigned char)(*str), map))
1298 *start++ = *str;
1299 else {
1300 if (start + 3 >= stop)
1301 break;
1302 *start++ = escape;
1303 *start++ = hextab[(*str >> 4) & 15];
1304 *start++ = hextab[*str & 15];
1305 }
1306 str++;
1307 }
1308 *start = '\0';
1309 }
1310 return start;
1311}
1312
Thierry FOURNIERddea6262015-05-28 16:00:28 +02001313/* Check a string for using it in a CSV output format. If the string contains
1314 * one of the following four char <">, <,>, CR or LF, the string is
1315 * encapsulated between <"> and the <"> are escaped by a <""> sequence.
1316 * <str> is the input string to be escaped. The function assumes that
1317 * the input string is null-terminated.
1318 *
1319 * If <quote> is 0, the result is returned escaped but without double quote.
1320 * Is it useful if the escaped string is used between double quotes in the
1321 * format.
1322 *
1323 * printf("..., \"%s\", ...\r\n", csv_enc(str, 0));
1324 *
1325 * If the <quote> is 1, the converter put the quotes only if any character is
1326 * escaped. If the <quote> is 2, the converter put always the quotes.
1327 *
1328 * <output> is a struct chunk used for storing the output string if any
1329 * change will be done.
1330 *
1331 * The function returns the converted string on this output. If an error
1332 * occurs, the function return an empty string. This type of output is useful
1333 * for using the function directly as printf() argument.
1334 *
1335 * If the output buffer is too short to contain the input string, the result
1336 * is truncated.
1337 */
1338const char *csv_enc(const char *str, int quote, struct chunk *output)
1339{
1340 char *end = output->str + output->size;
1341 char *out = output->str + 1; /* +1 for reserving space for a first <"> */
1342
1343 while (*str && out < end - 2) { /* -2 for reserving space for <"> and \0. */
1344 *out = *str;
1345 if (*str == '"') {
1346 if (quote == 1)
1347 quote = 2;
1348 out++;
1349 if (out >= end - 2) {
1350 out--;
1351 break;
1352 }
1353 *out = '"';
1354 }
1355 if (quote == 1 && ( *str == '\r' || *str == '\n' || *str == ',') )
1356 quote = 2;
1357 out++;
1358 str++;
1359 }
1360
1361 if (quote == 1)
1362 quote = 0;
1363
1364 if (!quote) {
1365 *out = '\0';
1366 return output->str + 1;
1367 }
1368
1369 /* else quote == 2 */
1370 *output->str = '"';
1371 *out = '"';
1372 out++;
1373 *out = '\0';
1374 return output->str;
1375}
1376
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001377/* Decode an URL-encoded string in-place. The resulting string might
1378 * be shorter. If some forbidden characters are found, the conversion is
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001379 * aborted, the string is truncated before the issue and a negative value is
1380 * returned, otherwise the operation returns the length of the decoded string.
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001381 */
1382int url_decode(char *string)
1383{
1384 char *in, *out;
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001385 int ret = -1;
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001386
1387 in = string;
1388 out = string;
1389 while (*in) {
1390 switch (*in) {
1391 case '+' :
1392 *out++ = ' ';
1393 break;
1394 case '%' :
1395 if (!ishex(in[1]) || !ishex(in[2]))
1396 goto end;
1397 *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]);
1398 in += 2;
1399 break;
1400 default:
1401 *out++ = *in;
1402 break;
1403 }
1404 in++;
1405 }
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001406 ret = out - string; /* success */
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001407 end:
1408 *out = 0;
1409 return ret;
1410}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001411
Willy Tarreau6911fa42007-03-04 18:06:08 +01001412unsigned int str2ui(const char *s)
1413{
1414 return __str2ui(s);
1415}
1416
1417unsigned int str2uic(const char *s)
1418{
1419 return __str2uic(s);
1420}
1421
1422unsigned int strl2ui(const char *s, int len)
1423{
1424 return __strl2ui(s, len);
1425}
1426
1427unsigned int strl2uic(const char *s, int len)
1428{
1429 return __strl2uic(s, len);
1430}
1431
Willy Tarreau4ec83cd2010-10-15 23:19:55 +02001432unsigned int read_uint(const char **s, const char *end)
1433{
1434 return __read_uint(s, end);
1435}
1436
Willy Tarreau6911fa42007-03-04 18:06:08 +01001437/* This one is 7 times faster than strtol() on athlon with checks.
1438 * It returns the value of the number composed of all valid digits read,
1439 * and can process negative numbers too.
1440 */
1441int strl2ic(const char *s, int len)
1442{
1443 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001444 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001445
1446 if (len > 0) {
1447 if (*s != '-') {
1448 /* positive number */
1449 while (len-- > 0) {
1450 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001451 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001452 if (j > 9)
1453 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001454 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001455 }
1456 } else {
1457 /* negative number */
1458 s++;
1459 while (--len > 0) {
1460 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001461 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001462 if (j > 9)
1463 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001464 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001465 }
1466 }
1467 }
1468 return i;
1469}
1470
1471
1472/* This function reads exactly <len> chars from <s> and converts them to a
1473 * signed integer which it stores into <ret>. It accurately detects any error
1474 * (truncated string, invalid chars, overflows). It is meant to be used in
1475 * applications designed for hostile environments. It returns zero when the
1476 * number has successfully been converted, non-zero otherwise. When an error
1477 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
1478 * faster than strtol().
1479 */
1480int strl2irc(const char *s, int len, int *ret)
1481{
1482 int i = 0;
1483 int j;
1484
1485 if (!len)
1486 return 1;
1487
1488 if (*s != '-') {
1489 /* positive number */
1490 while (len-- > 0) {
1491 j = (*s++) - '0';
1492 if (j > 9) return 1; /* invalid char */
1493 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
1494 i = i * 10;
1495 if (i + j < i) return 1; /* check for addition overflow */
1496 i = i + j;
1497 }
1498 } else {
1499 /* negative number */
1500 s++;
1501 while (--len > 0) {
1502 j = (*s++) - '0';
1503 if (j > 9) return 1; /* invalid char */
1504 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
1505 i = i * 10;
1506 if (i - j > i) return 1; /* check for subtract overflow */
1507 i = i - j;
1508 }
1509 }
1510 *ret = i;
1511 return 0;
1512}
1513
1514
1515/* This function reads exactly <len> chars from <s> and converts them to a
1516 * signed integer which it stores into <ret>. It accurately detects any error
1517 * (truncated string, invalid chars, overflows). It is meant to be used in
1518 * applications designed for hostile environments. It returns zero when the
1519 * number has successfully been converted, non-zero otherwise. When an error
1520 * is returned, the <ret> value is left untouched. It is about 3 times slower
1521 * than str2irc().
1522 */
Willy Tarreau6911fa42007-03-04 18:06:08 +01001523
1524int strl2llrc(const char *s, int len, long long *ret)
1525{
1526 long long i = 0;
1527 int j;
1528
1529 if (!len)
1530 return 1;
1531
1532 if (*s != '-') {
1533 /* positive number */
1534 while (len-- > 0) {
1535 j = (*s++) - '0';
1536 if (j > 9) return 1; /* invalid char */
1537 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
1538 i = i * 10LL;
1539 if (i + j < i) return 1; /* check for addition overflow */
1540 i = i + j;
1541 }
1542 } else {
1543 /* negative number */
1544 s++;
1545 while (--len > 0) {
1546 j = (*s++) - '0';
1547 if (j > 9) return 1; /* invalid char */
1548 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
1549 i = i * 10LL;
1550 if (i - j > i) return 1; /* check for subtract overflow */
1551 i = i - j;
1552 }
1553 }
1554 *ret = i;
1555 return 0;
1556}
1557
Thierry FOURNIER511e9472014-01-23 17:40:34 +01001558/* This function is used with pat_parse_dotted_ver(). It converts a string
1559 * composed by two number separated by a dot. Each part must contain in 16 bits
1560 * because internally they will be represented as a 32-bit quantity stored in
1561 * a 64-bit integer. It returns zero when the number has successfully been
1562 * converted, non-zero otherwise. When an error is returned, the <ret> value
1563 * is left untouched.
1564 *
1565 * "1.3" -> 0x0000000000010003
1566 * "65535.65535" -> 0x00000000ffffffff
1567 */
1568int strl2llrc_dotted(const char *text, int len, long long *ret)
1569{
1570 const char *end = &text[len];
1571 const char *p;
1572 long long major, minor;
1573
1574 /* Look for dot. */
1575 for (p = text; p < end; p++)
1576 if (*p == '.')
1577 break;
1578
1579 /* Convert major. */
1580 if (strl2llrc(text, p - text, &major) != 0)
1581 return 1;
1582
1583 /* Check major. */
1584 if (major >= 65536)
1585 return 1;
1586
1587 /* Convert minor. */
1588 minor = 0;
1589 if (p < end)
1590 if (strl2llrc(p + 1, end - (p + 1), &minor) != 0)
1591 return 1;
1592
1593 /* Check minor. */
1594 if (minor >= 65536)
1595 return 1;
1596
1597 /* Compose value. */
1598 *ret = (major << 16) | (minor & 0xffff);
1599 return 0;
1600}
1601
Willy Tarreaua0d37b62007-12-02 22:00:35 +01001602/* This function parses a time value optionally followed by a unit suffix among
1603 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
1604 * expected by the caller. The computation does its best to avoid overflows.
1605 * The value is returned in <ret> if everything is fine, and a NULL is returned
1606 * by the function. In case of error, a pointer to the error is returned and
1607 * <ret> is left untouched. Values are automatically rounded up when needed.
1608 */
1609const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
1610{
1611 unsigned imult, idiv;
1612 unsigned omult, odiv;
1613 unsigned value;
1614
1615 omult = odiv = 1;
1616
1617 switch (unit_flags & TIME_UNIT_MASK) {
1618 case TIME_UNIT_US: omult = 1000000; break;
1619 case TIME_UNIT_MS: omult = 1000; break;
1620 case TIME_UNIT_S: break;
1621 case TIME_UNIT_MIN: odiv = 60; break;
1622 case TIME_UNIT_HOUR: odiv = 3600; break;
1623 case TIME_UNIT_DAY: odiv = 86400; break;
1624 default: break;
1625 }
1626
1627 value = 0;
1628
1629 while (1) {
1630 unsigned int j;
1631
1632 j = *text - '0';
1633 if (j > 9)
1634 break;
1635 text++;
1636 value *= 10;
1637 value += j;
1638 }
1639
1640 imult = idiv = 1;
1641 switch (*text) {
1642 case '\0': /* no unit = default unit */
1643 imult = omult = idiv = odiv = 1;
1644 break;
1645 case 's': /* second = unscaled unit */
1646 break;
1647 case 'u': /* microsecond : "us" */
1648 if (text[1] == 's') {
1649 idiv = 1000000;
1650 text++;
1651 }
1652 break;
1653 case 'm': /* millisecond : "ms" or minute: "m" */
1654 if (text[1] == 's') {
1655 idiv = 1000;
1656 text++;
1657 } else
1658 imult = 60;
1659 break;
1660 case 'h': /* hour : "h" */
1661 imult = 3600;
1662 break;
1663 case 'd': /* day : "d" */
1664 imult = 86400;
1665 break;
1666 default:
1667 return text;
1668 break;
1669 }
1670
1671 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
1672 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
1673 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
1674 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
1675
1676 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
1677 *ret = value;
1678 return NULL;
1679}
Willy Tarreau6911fa42007-03-04 18:06:08 +01001680
Emeric Brun39132b22010-01-04 14:57:24 +01001681/* this function converts the string starting at <text> to an unsigned int
1682 * stored in <ret>. If an error is detected, the pointer to the unexpected
1683 * character is returned. If the conversio is succesful, NULL is returned.
1684 */
1685const char *parse_size_err(const char *text, unsigned *ret) {
1686 unsigned value = 0;
1687
1688 while (1) {
1689 unsigned int j;
1690
1691 j = *text - '0';
1692 if (j > 9)
1693 break;
1694 if (value > ~0U / 10)
1695 return text;
1696 value *= 10;
1697 if (value > (value + j))
1698 return text;
1699 value += j;
1700 text++;
1701 }
1702
1703 switch (*text) {
1704 case '\0':
1705 break;
1706 case 'K':
1707 case 'k':
1708 if (value > ~0U >> 10)
1709 return text;
1710 value = value << 10;
1711 break;
1712 case 'M':
1713 case 'm':
1714 if (value > ~0U >> 20)
1715 return text;
1716 value = value << 20;
1717 break;
1718 case 'G':
1719 case 'g':
1720 if (value > ~0U >> 30)
1721 return text;
1722 value = value << 30;
1723 break;
1724 default:
1725 return text;
1726 }
1727
Godbach58048a22015-01-28 17:36:16 +08001728 if (*text != '\0' && *++text != '\0')
1729 return text;
1730
Emeric Brun39132b22010-01-04 14:57:24 +01001731 *ret = value;
1732 return NULL;
1733}
1734
Willy Tarreau126d4062013-12-03 17:50:47 +01001735/*
1736 * Parse binary string written in hexadecimal (source) and store the decoded
1737 * result into binstr and set binstrlen to the lengh of binstr. Memory for
1738 * binstr is allocated by the function. In case of error, returns 0 with an
Thierry FOURNIERee330af2014-01-21 11:36:14 +01001739 * error message in err. In succes case, it returns the consumed length.
Willy Tarreau126d4062013-12-03 17:50:47 +01001740 */
1741int parse_binary(const char *source, char **binstr, int *binstrlen, char **err)
1742{
1743 int len;
1744 const char *p = source;
1745 int i,j;
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001746 int alloc;
Willy Tarreau126d4062013-12-03 17:50:47 +01001747
1748 len = strlen(source);
1749 if (len % 2) {
1750 memprintf(err, "an even number of hex digit is expected");
1751 return 0;
1752 }
1753
1754 len = len >> 1;
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001755
Willy Tarreau126d4062013-12-03 17:50:47 +01001756 if (!*binstr) {
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001757 *binstr = calloc(len, sizeof(char));
1758 if (!*binstr) {
1759 memprintf(err, "out of memory while loading string pattern");
1760 return 0;
1761 }
1762 alloc = 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001763 }
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001764 else {
1765 if (*binstrlen < len) {
1766 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
1767 len, *binstrlen);
1768 return 0;
1769 }
1770 alloc = 0;
1771 }
1772 *binstrlen = len;
Willy Tarreau126d4062013-12-03 17:50:47 +01001773
1774 i = j = 0;
1775 while (j < len) {
1776 if (!ishex(p[i++]))
1777 goto bad_input;
1778 if (!ishex(p[i++]))
1779 goto bad_input;
1780 (*binstr)[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
1781 }
Thierry FOURNIERee330af2014-01-21 11:36:14 +01001782 return len << 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001783
1784bad_input:
1785 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001786 if (alloc)
1787 free(binstr);
Willy Tarreau126d4062013-12-03 17:50:47 +01001788 return 0;
1789}
1790
Willy Tarreau946ba592009-05-10 15:41:18 +02001791/* copies at most <n> characters from <src> and always terminates with '\0' */
1792char *my_strndup(const char *src, int n)
1793{
1794 int len = 0;
1795 char *ret;
1796
1797 while (len < n && src[len])
1798 len++;
1799
1800 ret = (char *)malloc(len + 1);
1801 if (!ret)
1802 return ret;
1803 memcpy(ret, src, len);
1804 ret[len] = '\0';
1805 return ret;
1806}
1807
Baptiste Assmannbb77c8e2013-10-06 23:24:13 +02001808/*
1809 * search needle in haystack
1810 * returns the pointer if found, returns NULL otherwise
1811 */
1812const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen)
1813{
1814 const void *c = NULL;
1815 unsigned char f;
1816
1817 if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen))
1818 return NULL;
1819
1820 f = *(char *)needle;
1821 c = haystack;
1822 while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) {
1823 if ((haystacklen - (c - haystack)) < needlelen)
1824 return NULL;
1825
1826 if (memcmp(c, needle, needlelen) == 0)
1827 return c;
1828 ++c;
1829 }
1830 return NULL;
1831}
1832
Willy Tarreau482b00d2009-10-04 22:48:42 +02001833/* This function returns the first unused key greater than or equal to <key> in
1834 * ID tree <root>. Zero is returned if no place is found.
1835 */
1836unsigned int get_next_id(struct eb_root *root, unsigned int key)
1837{
1838 struct eb32_node *used;
1839
1840 do {
1841 used = eb32_lookup_ge(root, key);
1842 if (!used || used->key > key)
1843 return key; /* key is available */
1844 key++;
1845 } while (key);
1846 return key;
1847}
1848
Willy Tarreau348238b2010-01-18 15:05:57 +01001849/* This function compares a sample word possibly followed by blanks to another
1850 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
1851 * otherwise zero. This intends to be used when checking HTTP headers for some
1852 * values. Note that it validates a word followed only by blanks but does not
1853 * validate a word followed by blanks then other chars.
1854 */
1855int word_match(const char *sample, int slen, const char *word, int wlen)
1856{
1857 if (slen < wlen)
1858 return 0;
1859
1860 while (wlen) {
1861 char c = *sample ^ *word;
1862 if (c && c != ('A' ^ 'a'))
1863 return 0;
1864 sample++;
1865 word++;
1866 slen--;
1867 wlen--;
1868 }
1869
1870 while (slen) {
1871 if (*sample != ' ' && *sample != '\t')
1872 return 0;
1873 sample++;
1874 slen--;
1875 }
1876 return 1;
1877}
Willy Tarreau482b00d2009-10-04 22:48:42 +02001878
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001879/* Converts any text-formatted IPv4 address to a host-order IPv4 address. It
1880 * is particularly fast because it avoids expensive operations such as
1881 * multiplies, which are optimized away at the end. It requires a properly
1882 * formated address though (3 points).
1883 */
1884unsigned int inetaddr_host(const char *text)
1885{
1886 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1887 register unsigned int dig100, dig10, dig1;
1888 int s;
1889 const char *p, *d;
1890
1891 dig1 = dig10 = dig100 = ascii_zero;
1892 s = 24;
1893
1894 p = text;
1895 while (1) {
1896 if (((unsigned)(*p - '0')) <= 9) {
1897 p++;
1898 continue;
1899 }
1900
1901 /* here, we have a complete byte between <text> and <p> (exclusive) */
1902 if (p == text)
1903 goto end;
1904
1905 d = p - 1;
1906 dig1 |= (unsigned int)(*d << s);
1907 if (d == text)
1908 goto end;
1909
1910 d--;
1911 dig10 |= (unsigned int)(*d << s);
1912 if (d == text)
1913 goto end;
1914
1915 d--;
1916 dig100 |= (unsigned int)(*d << s);
1917 end:
1918 if (!s || *p != '.')
1919 break;
1920
1921 s -= 8;
1922 text = ++p;
1923 }
1924
1925 dig100 -= ascii_zero;
1926 dig10 -= ascii_zero;
1927 dig1 -= ascii_zero;
1928 return ((dig100 * 10) + dig10) * 10 + dig1;
1929}
1930
1931/*
1932 * Idem except the first unparsed character has to be passed in <stop>.
1933 */
1934unsigned int inetaddr_host_lim(const char *text, const char *stop)
1935{
1936 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1937 register unsigned int dig100, dig10, dig1;
1938 int s;
1939 const char *p, *d;
1940
1941 dig1 = dig10 = dig100 = ascii_zero;
1942 s = 24;
1943
1944 p = text;
1945 while (1) {
1946 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1947 p++;
1948 continue;
1949 }
1950
1951 /* here, we have a complete byte between <text> and <p> (exclusive) */
1952 if (p == text)
1953 goto end;
1954
1955 d = p - 1;
1956 dig1 |= (unsigned int)(*d << s);
1957 if (d == text)
1958 goto end;
1959
1960 d--;
1961 dig10 |= (unsigned int)(*d << s);
1962 if (d == text)
1963 goto end;
1964
1965 d--;
1966 dig100 |= (unsigned int)(*d << s);
1967 end:
1968 if (!s || p == stop || *p != '.')
1969 break;
1970
1971 s -= 8;
1972 text = ++p;
1973 }
1974
1975 dig100 -= ascii_zero;
1976 dig10 -= ascii_zero;
1977 dig1 -= ascii_zero;
1978 return ((dig100 * 10) + dig10) * 10 + dig1;
1979}
1980
1981/*
1982 * Idem except the pointer to first unparsed byte is returned into <ret> which
1983 * must not be NULL.
1984 */
Willy Tarreau74172752010-10-15 23:21:42 +02001985unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret)
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001986{
1987 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1988 register unsigned int dig100, dig10, dig1;
1989 int s;
Willy Tarreau74172752010-10-15 23:21:42 +02001990 char *p, *d;
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001991
1992 dig1 = dig10 = dig100 = ascii_zero;
1993 s = 24;
1994
1995 p = text;
1996 while (1) {
1997 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1998 p++;
1999 continue;
2000 }
2001
2002 /* here, we have a complete byte between <text> and <p> (exclusive) */
2003 if (p == text)
2004 goto end;
2005
2006 d = p - 1;
2007 dig1 |= (unsigned int)(*d << s);
2008 if (d == text)
2009 goto end;
2010
2011 d--;
2012 dig10 |= (unsigned int)(*d << s);
2013 if (d == text)
2014 goto end;
2015
2016 d--;
2017 dig100 |= (unsigned int)(*d << s);
2018 end:
2019 if (!s || p == stop || *p != '.')
2020 break;
2021
2022 s -= 8;
2023 text = ++p;
2024 }
2025
2026 *ret = p;
2027 dig100 -= ascii_zero;
2028 dig10 -= ascii_zero;
2029 dig1 -= ascii_zero;
2030 return ((dig100 * 10) + dig10) * 10 + dig1;
2031}
2032
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02002033/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
2034 * or the number of chars read in case of success. Maybe this could be replaced
2035 * by one of the functions above. Also, apparently this function does not support
2036 * hosts above 255 and requires exactly 4 octets.
Willy Tarreau075415a2013-12-12 11:29:39 +01002037 * The destination is only modified on success.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02002038 */
2039int buf2ip(const char *buf, size_t len, struct in_addr *dst)
2040{
2041 const char *addr;
2042 int saw_digit, octets, ch;
2043 u_char tmp[4], *tp;
2044 const char *cp = buf;
2045
2046 saw_digit = 0;
2047 octets = 0;
2048 *(tp = tmp) = 0;
2049
2050 for (addr = buf; addr - buf < len; addr++) {
2051 unsigned char digit = (ch = *addr) - '0';
2052
2053 if (digit > 9 && ch != '.')
2054 break;
2055
2056 if (digit <= 9) {
2057 u_int new = *tp * 10 + digit;
2058
2059 if (new > 255)
2060 return 0;
2061
2062 *tp = new;
2063
2064 if (!saw_digit) {
2065 if (++octets > 4)
2066 return 0;
2067 saw_digit = 1;
2068 }
2069 } else if (ch == '.' && saw_digit) {
2070 if (octets == 4)
2071 return 0;
2072
2073 *++tp = 0;
2074 saw_digit = 0;
2075 } else
2076 return 0;
2077 }
2078
2079 if (octets < 4)
2080 return 0;
2081
2082 memcpy(&dst->s_addr, tmp, 4);
2083 return addr - cp;
2084}
2085
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002086/* This function converts the string in <buf> of the len <len> to
2087 * struct in6_addr <dst> which must be allocated by the caller.
2088 * This function returns 1 in success case, otherwise zero.
Willy Tarreau075415a2013-12-12 11:29:39 +01002089 * The destination is only modified on success.
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002090 */
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002091int buf2ip6(const char *buf, size_t len, struct in6_addr *dst)
2092{
Thierry FOURNIERcd659912013-12-11 12:33:54 +01002093 char null_term_ip6[INET6_ADDRSTRLEN + 1];
Willy Tarreau075415a2013-12-12 11:29:39 +01002094 struct in6_addr out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002095
Thierry FOURNIERcd659912013-12-11 12:33:54 +01002096 if (len > INET6_ADDRSTRLEN)
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002097 return 0;
2098
2099 memcpy(null_term_ip6, buf, len);
2100 null_term_ip6[len] = '\0';
2101
Willy Tarreau075415a2013-12-12 11:29:39 +01002102 if (!inet_pton(AF_INET6, null_term_ip6, &out))
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002103 return 0;
2104
Willy Tarreau075415a2013-12-12 11:29:39 +01002105 *dst = out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002106 return 1;
2107}
2108
Willy Tarreauacf95772010-06-14 19:09:21 +02002109/* To be used to quote config arg positions. Returns the short string at <ptr>
2110 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
2111 * if ptr is NULL or empty. The string is locally allocated.
2112 */
2113const char *quote_arg(const char *ptr)
2114{
2115 static char val[32];
2116 int i;
2117
2118 if (!ptr || !*ptr)
2119 return "end of line";
2120 val[0] = '\'';
Willy Tarreaude2dd6b2013-01-24 02:14:42 +01002121 for (i = 1; i < sizeof(val) - 2 && *ptr; i++)
Willy Tarreauacf95772010-06-14 19:09:21 +02002122 val[i] = *ptr++;
2123 val[i++] = '\'';
2124 val[i] = '\0';
2125 return val;
2126}
2127
Willy Tarreau5b180202010-07-18 10:40:48 +02002128/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
2129int get_std_op(const char *str)
2130{
2131 int ret = -1;
2132
2133 if (*str == 'e' && str[1] == 'q')
2134 ret = STD_OP_EQ;
2135 else if (*str == 'n' && str[1] == 'e')
2136 ret = STD_OP_NE;
2137 else if (*str == 'l') {
2138 if (str[1] == 'e') ret = STD_OP_LE;
2139 else if (str[1] == 't') ret = STD_OP_LT;
2140 }
2141 else if (*str == 'g') {
2142 if (str[1] == 'e') ret = STD_OP_GE;
2143 else if (str[1] == 't') ret = STD_OP_GT;
2144 }
2145
2146 if (ret == -1 || str[2] != '\0')
2147 return -1;
2148 return ret;
2149}
2150
Willy Tarreau4c14eaa2010-11-24 14:01:45 +01002151/* hash a 32-bit integer to another 32-bit integer */
2152unsigned int full_hash(unsigned int a)
2153{
2154 return __full_hash(a);
2155}
2156
David du Colombier4f92d322011-03-24 11:09:31 +01002157/* Return non-zero if IPv4 address is part of the network,
2158 * otherwise zero.
2159 */
2160int in_net_ipv4(struct in_addr *addr, struct in_addr *mask, struct in_addr *net)
2161{
2162 return((addr->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr));
2163}
2164
2165/* Return non-zero if IPv6 address is part of the network,
2166 * otherwise zero.
2167 */
2168int in_net_ipv6(struct in6_addr *addr, struct in6_addr *mask, struct in6_addr *net)
2169{
2170 int i;
2171
2172 for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++)
2173 if (((((int *)addr)[i] & ((int *)mask)[i])) !=
2174 (((int *)net)[i] & ((int *)mask)[i]))
2175 return 0;
2176 return 1;
2177}
2178
2179/* RFC 4291 prefix */
2180const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00,
2181 0x00, 0x00, 0x00, 0x00,
2182 0x00, 0x00, 0xFF, 0xFF };
2183
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002184/* Map IPv4 adress on IPv6 address, as specified in RFC 3513.
2185 * Input and output may overlap.
2186 */
David du Colombier4f92d322011-03-24 11:09:31 +01002187void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr)
2188{
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002189 struct in_addr tmp_addr;
2190
2191 tmp_addr.s_addr = sin_addr->s_addr;
David du Colombier4f92d322011-03-24 11:09:31 +01002192 memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx));
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002193 memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4);
David du Colombier4f92d322011-03-24 11:09:31 +01002194}
2195
2196/* Map IPv6 adress on IPv4 address, as specified in RFC 3513.
2197 * Return true if conversion is possible and false otherwise.
2198 */
2199int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr)
2200{
2201 if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) {
2202 memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]),
2203 sizeof(struct in_addr));
2204 return 1;
2205 }
2206
2207 return 0;
2208}
2209
William Lallemand421f5b52012-02-06 18:15:57 +01002210char *human_time(int t, short hz_div) {
2211 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
2212 char *p = rv;
Willy Tarreau761b3d52014-04-14 14:53:06 +02002213 char *end = rv + sizeof(rv);
William Lallemand421f5b52012-02-06 18:15:57 +01002214 int cnt=2; // print two numbers
2215
2216 if (unlikely(t < 0 || hz_div <= 0)) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002217 snprintf(p, end - p, "?");
William Lallemand421f5b52012-02-06 18:15:57 +01002218 return rv;
2219 }
2220
2221 if (unlikely(hz_div > 1))
2222 t /= hz_div;
2223
2224 if (t >= DAY) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002225 p += snprintf(p, end - p, "%dd", t / DAY);
William Lallemand421f5b52012-02-06 18:15:57 +01002226 cnt--;
2227 }
2228
2229 if (cnt && t % DAY / HOUR) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002230 p += snprintf(p, end - p, "%dh", t % DAY / HOUR);
William Lallemand421f5b52012-02-06 18:15:57 +01002231 cnt--;
2232 }
2233
2234 if (cnt && t % HOUR / MINUTE) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002235 p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE);
William Lallemand421f5b52012-02-06 18:15:57 +01002236 cnt--;
2237 }
2238
2239 if ((cnt && t % MINUTE) || !t) // also display '0s'
Willy Tarreau761b3d52014-04-14 14:53:06 +02002240 p += snprintf(p, end - p, "%ds", t % MINUTE / SEC);
William Lallemand421f5b52012-02-06 18:15:57 +01002241
2242 return rv;
2243}
2244
2245const char *monthname[12] = {
2246 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2247 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2248};
2249
2250/* date2str_log: write a date in the format :
2251 * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d",
2252 * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2253 * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000);
2254 *
2255 * without using sprintf. return a pointer to the last char written (\0) or
2256 * NULL if there isn't enough space.
2257 */
2258char *date2str_log(char *dst, struct tm *tm, struct timeval *date, size_t size)
2259{
2260
2261 if (size < 25) /* the size is fixed: 24 chars + \0 */
2262 return NULL;
2263
2264 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2265 *dst++ = '/';
2266 memcpy(dst, monthname[tm->tm_mon], 3); // month
2267 dst += 3;
2268 *dst++ = '/';
2269 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2270 *dst++ = ':';
2271 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2272 *dst++ = ':';
2273 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2274 *dst++ = ':';
2275 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2276 *dst++ = '.';
2277 utoa_pad((unsigned int)(date->tv_usec/1000), dst, 4); // millisecondes
2278 dst += 3; // only the 3 first digits
2279 *dst = '\0';
2280
2281 return dst;
2282}
2283
2284/* gmt2str_log: write a date in the format :
2285 * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf
2286 * return a pointer to the last char written (\0) or
2287 * NULL if there isn't enough space.
2288 */
2289char *gmt2str_log(char *dst, struct tm *tm, size_t size)
2290{
Yuxans Yao4e25b012012-10-19 10:36:09 +08002291 if (size < 27) /* the size is fixed: 26 chars + \0 */
William Lallemand421f5b52012-02-06 18:15:57 +01002292 return NULL;
2293
2294 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2295 *dst++ = '/';
2296 memcpy(dst, monthname[tm->tm_mon], 3); // month
2297 dst += 3;
2298 *dst++ = '/';
2299 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2300 *dst++ = ':';
2301 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2302 *dst++ = ':';
2303 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2304 *dst++ = ':';
2305 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2306 *dst++ = ' ';
2307 *dst++ = '+';
2308 *dst++ = '0';
2309 *dst++ = '0';
2310 *dst++ = '0';
2311 *dst++ = '0';
2312 *dst = '\0';
2313
2314 return dst;
2315}
2316
Yuxans Yao4e25b012012-10-19 10:36:09 +08002317/* localdate2str_log: write a date in the format :
2318 * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf
2319 * * return a pointer to the last char written (\0) or
2320 * * NULL if there isn't enough space.
2321 */
2322char *localdate2str_log(char *dst, struct tm *tm, size_t size)
2323{
2324 if (size < 27) /* the size is fixed: 26 chars + \0 */
2325 return NULL;
2326
2327 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2328 *dst++ = '/';
2329 memcpy(dst, monthname[tm->tm_mon], 3); // month
2330 dst += 3;
2331 *dst++ = '/';
2332 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2333 *dst++ = ':';
2334 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2335 *dst++ = ':';
2336 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2337 *dst++ = ':';
2338 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2339 *dst++ = ' ';
2340 memcpy(dst, localtimezone, 5); // timezone
2341 dst += 5;
2342 *dst = '\0';
2343
2344 return dst;
2345}
2346
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002347/* Dynamically allocates a string of the proper length to hold the formatted
2348 * output. NULL is returned on error. The caller is responsible for freeing the
2349 * memory area using free(). The resulting string is returned in <out> if the
2350 * pointer is not NULL. A previous version of <out> might be used to build the
2351 * new string, and it will be freed before returning if it is not NULL, which
2352 * makes it possible to build complex strings from iterative calls without
2353 * having to care about freeing intermediate values, as in the example below :
2354 *
2355 * memprintf(&err, "invalid argument: '%s'", arg);
2356 * ...
2357 * memprintf(&err, "parser said : <%s>\n", *err);
2358 * ...
2359 * free(*err);
2360 *
2361 * This means that <err> must be initialized to NULL before first invocation.
2362 * The return value also holds the allocated string, which eases error checking
2363 * and immediate consumption. If the output pointer is not used, NULL must be
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002364 * passed instead and it will be ignored. The returned message will then also
2365 * be NULL so that the caller does not have to bother with freeing anything.
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002366 *
2367 * It is also convenient to use it without any free except the last one :
2368 * err = NULL;
2369 * if (!fct1(err)) report(*err);
2370 * if (!fct2(err)) report(*err);
2371 * if (!fct3(err)) report(*err);
2372 * free(*err);
2373 */
2374char *memprintf(char **out, const char *format, ...)
2375{
2376 va_list args;
2377 char *ret = NULL;
2378 int allocated = 0;
2379 int needed = 0;
2380
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002381 if (!out)
2382 return NULL;
2383
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002384 do {
2385 /* vsnprintf() will return the required length even when the
2386 * target buffer is NULL. We do this in a loop just in case
2387 * intermediate evaluations get wrong.
2388 */
2389 va_start(args, format);
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002390 needed = vsnprintf(ret, allocated, format, args);
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002391 va_end(args);
2392
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002393 if (needed < allocated) {
2394 /* Note: on Solaris 8, the first iteration always
2395 * returns -1 if allocated is zero, so we force a
2396 * retry.
2397 */
2398 if (!allocated)
2399 needed = 0;
2400 else
2401 break;
2402 }
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002403
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002404 allocated = needed + 1;
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002405 ret = realloc(ret, allocated);
2406 } while (ret);
2407
2408 if (needed < 0) {
2409 /* an error was encountered */
2410 free(ret);
2411 ret = NULL;
2412 }
2413
2414 if (out) {
2415 free(*out);
2416 *out = ret;
2417 }
2418
2419 return ret;
2420}
William Lallemand421f5b52012-02-06 18:15:57 +01002421
Willy Tarreau21c705b2012-09-14 11:40:36 +02002422/* Used to add <level> spaces before each line of <out>, unless there is only one line.
2423 * The input argument is automatically freed and reassigned. The result will have to be
Willy Tarreau70eec382012-10-10 08:56:47 +02002424 * freed by the caller. It also supports being passed a NULL which results in the same
2425 * output.
Willy Tarreau21c705b2012-09-14 11:40:36 +02002426 * Example of use :
2427 * parse(cmd, &err); (callee: memprintf(&err, ...))
2428 * fprintf(stderr, "Parser said: %s\n", indent_error(&err));
2429 * free(err);
2430 */
2431char *indent_msg(char **out, int level)
2432{
2433 char *ret, *in, *p;
2434 int needed = 0;
2435 int lf = 0;
2436 int lastlf = 0;
2437 int len;
2438
Willy Tarreau70eec382012-10-10 08:56:47 +02002439 if (!out || !*out)
2440 return NULL;
2441
Willy Tarreau21c705b2012-09-14 11:40:36 +02002442 in = *out - 1;
2443 while ((in = strchr(in + 1, '\n')) != NULL) {
2444 lastlf = in - *out;
2445 lf++;
2446 }
2447
2448 if (!lf) /* single line, no LF, return it as-is */
2449 return *out;
2450
2451 len = strlen(*out);
2452
2453 if (lf == 1 && lastlf == len - 1) {
2454 /* single line, LF at end, strip it and return as-is */
2455 (*out)[lastlf] = 0;
2456 return *out;
2457 }
2458
2459 /* OK now we have at least one LF, we need to process the whole string
2460 * as a multi-line string. What we'll do :
2461 * - prefix with an LF if there is none
2462 * - add <level> spaces before each line
2463 * This means at most ( 1 + level + (len-lf) + lf*<1+level) ) =
2464 * 1 + level + len + lf * level = 1 + level * (lf + 1) + len.
2465 */
2466
2467 needed = 1 + level * (lf + 1) + len + 1;
2468 p = ret = malloc(needed);
2469 in = *out;
2470
2471 /* skip initial LFs */
2472 while (*in == '\n')
2473 in++;
2474
2475 /* copy each line, prefixed with LF and <level> spaces, and without the trailing LF */
2476 while (*in) {
2477 *p++ = '\n';
2478 memset(p, ' ', level);
2479 p += level;
2480 do {
2481 *p++ = *in++;
2482 } while (*in && *in != '\n');
2483 if (*in)
2484 in++;
2485 }
2486 *p = 0;
2487
2488 free(*out);
2489 *out = ret;
2490
2491 return ret;
2492}
2493
Willy Tarreaudad36a32013-03-11 01:20:04 +01002494/* Convert occurrences of environment variables in the input string to their
2495 * corresponding value. A variable is identified as a series of alphanumeric
2496 * characters or underscores following a '$' sign. The <in> string must be
2497 * free()able. NULL returns NULL. The resulting string might be reallocated if
2498 * some expansion is made. Variable names may also be enclosed into braces if
2499 * needed (eg: to concatenate alphanum characters).
2500 */
2501char *env_expand(char *in)
2502{
2503 char *txt_beg;
2504 char *out;
2505 char *txt_end;
2506 char *var_beg;
2507 char *var_end;
2508 char *value;
2509 char *next;
2510 int out_len;
2511 int val_len;
2512
2513 if (!in)
2514 return in;
2515
2516 value = out = NULL;
2517 out_len = 0;
2518
2519 txt_beg = in;
2520 do {
2521 /* look for next '$' sign in <in> */
2522 for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++);
2523
2524 if (!*txt_end && !out) /* end and no expansion performed */
2525 return in;
2526
2527 val_len = 0;
2528 next = txt_end;
2529 if (*txt_end == '$') {
2530 char save;
2531
2532 var_beg = txt_end + 1;
2533 if (*var_beg == '{')
2534 var_beg++;
2535
2536 var_end = var_beg;
2537 while (isalnum((int)(unsigned char)*var_end) || *var_end == '_') {
2538 var_end++;
2539 }
2540
2541 next = var_end;
2542 if (*var_end == '}' && (var_beg > txt_end + 1))
2543 next++;
2544
2545 /* get value of the variable name at this location */
2546 save = *var_end;
2547 *var_end = '\0';
2548 value = getenv(var_beg);
2549 *var_end = save;
2550 val_len = value ? strlen(value) : 0;
2551 }
2552
2553 out = realloc(out, out_len + (txt_end - txt_beg) + val_len + 1);
2554 if (txt_end > txt_beg) {
2555 memcpy(out + out_len, txt_beg, txt_end - txt_beg);
2556 out_len += txt_end - txt_beg;
2557 }
2558 if (val_len) {
2559 memcpy(out + out_len, value, val_len);
2560 out_len += val_len;
2561 }
2562 out[out_len] = 0;
2563 txt_beg = next;
2564 } while (*txt_beg);
2565
2566 /* here we know that <out> was allocated and that we don't need <in> anymore */
2567 free(in);
2568 return out;
2569}
2570
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002571
2572/* same as strstr() but case-insensitive and with limit length */
2573const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2)
2574{
2575 char *pptr, *sptr, *start;
Willy Tarreauc8746532014-05-28 23:05:07 +02002576 unsigned int slen, plen;
2577 unsigned int tmp1, tmp2;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002578
2579 if (str1 == NULL || len_str1 == 0) // search pattern into an empty string => search is not found
2580 return NULL;
2581
2582 if (str2 == NULL || len_str2 == 0) // pattern is empty => every str1 match
2583 return str1;
2584
2585 if (len_str1 < len_str2) // pattern is longer than string => search is not found
2586 return NULL;
2587
2588 for (tmp1 = 0, start = (char *)str1, pptr = (char *)str2, slen = len_str1, plen = len_str2; slen >= plen; start++, slen--) {
2589 while (toupper(*start) != toupper(*str2)) {
2590 start++;
2591 slen--;
2592 tmp1++;
2593
2594 if (tmp1 >= len_str1)
2595 return NULL;
2596
2597 /* if pattern longer than string */
2598 if (slen < plen)
2599 return NULL;
2600 }
2601
2602 sptr = start;
2603 pptr = (char *)str2;
2604
2605 tmp2 = 0;
2606 while (toupper(*sptr) == toupper(*pptr)) {
2607 sptr++;
2608 pptr++;
2609 tmp2++;
2610
2611 if (*pptr == '\0' || tmp2 == len_str2) /* end of pattern found */
2612 return start;
2613 if (*sptr == '\0' || tmp2 == len_str1) /* end of string found and the pattern is not fully found */
2614 return NULL;
2615 }
2616 }
2617 return NULL;
2618}
2619
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002620/* This function read the next valid utf8 char.
2621 * <s> is the byte srray to be decode, <len> is its length.
2622 * The function returns decoded char encoded like this:
2623 * The 4 msb are the return code (UTF8_CODE_*), the 4 lsb
2624 * are the length read. The decoded character is stored in <c>.
2625 */
2626unsigned char utf8_next(const char *s, int len, unsigned int *c)
2627{
2628 const unsigned char *p = (unsigned char *)s;
2629 int dec;
2630 unsigned char code = UTF8_CODE_OK;
2631
2632 if (len < 1)
2633 return UTF8_CODE_OK;
2634
2635 /* Check the type of UTF8 sequence
2636 *
2637 * 0... .... 0x00 <= x <= 0x7f : 1 byte: ascii char
2638 * 10.. .... 0x80 <= x <= 0xbf : invalid sequence
2639 * 110. .... 0xc0 <= x <= 0xdf : 2 bytes
2640 * 1110 .... 0xe0 <= x <= 0xef : 3 bytes
2641 * 1111 0... 0xf0 <= x <= 0xf7 : 4 bytes
2642 * 1111 10.. 0xf8 <= x <= 0xfb : 5 bytes
2643 * 1111 110. 0xfc <= x <= 0xfd : 6 bytes
2644 * 1111 111. 0xfe <= x <= 0xff : invalid sequence
2645 */
2646 switch (*p) {
2647 case 0x00 ... 0x7f:
2648 *c = *p;
2649 return UTF8_CODE_OK | 1;
2650
2651 case 0x80 ... 0xbf:
2652 *c = *p;
2653 return UTF8_CODE_BADSEQ | 1;
2654
2655 case 0xc0 ... 0xdf:
2656 if (len < 2) {
2657 *c = *p;
2658 return UTF8_CODE_BADSEQ | 1;
2659 }
2660 *c = *p & 0x1f;
2661 dec = 1;
2662 break;
2663
2664 case 0xe0 ... 0xef:
2665 if (len < 3) {
2666 *c = *p;
2667 return UTF8_CODE_BADSEQ | 1;
2668 }
2669 *c = *p & 0x0f;
2670 dec = 2;
2671 break;
2672
2673 case 0xf0 ... 0xf7:
2674 if (len < 4) {
2675 *c = *p;
2676 return UTF8_CODE_BADSEQ | 1;
2677 }
2678 *c = *p & 0x07;
2679 dec = 3;
2680 break;
2681
2682 case 0xf8 ... 0xfb:
2683 if (len < 5) {
2684 *c = *p;
2685 return UTF8_CODE_BADSEQ | 1;
2686 }
2687 *c = *p & 0x03;
2688 dec = 4;
2689 break;
2690
2691 case 0xfc ... 0xfd:
2692 if (len < 6) {
2693 *c = *p;
2694 return UTF8_CODE_BADSEQ | 1;
2695 }
2696 *c = *p & 0x01;
2697 dec = 5;
2698 break;
2699
2700 case 0xfe ... 0xff:
2701 default:
2702 *c = *p;
2703 return UTF8_CODE_BADSEQ | 1;
2704 }
2705
2706 p++;
2707
2708 while (dec > 0) {
2709
2710 /* need 0x10 for the 2 first bits */
2711 if ( ( *p & 0xc0 ) != 0x80 )
2712 return UTF8_CODE_BADSEQ | ((p-(unsigned char *)s)&0xffff);
2713
2714 /* add data at char */
2715 *c = ( *c << 6 ) | ( *p & 0x3f );
2716
2717 dec--;
2718 p++;
2719 }
2720
2721 /* Check ovelong encoding.
2722 * 1 byte : 5 + 6 : 11 : 0x80 ... 0x7ff
2723 * 2 bytes : 4 + 6 + 6 : 16 : 0x800 ... 0xffff
2724 * 3 bytes : 3 + 6 + 6 + 6 : 21 : 0x10000 ... 0x1fffff
2725 */
Thierry FOURNIER9e7ec082015-03-12 19:32:38 +01002726 if (( *c <= 0x7f && (p-(unsigned char *)s) > 1) ||
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002727 (*c >= 0x80 && *c <= 0x7ff && (p-(unsigned char *)s) > 2) ||
2728 (*c >= 0x800 && *c <= 0xffff && (p-(unsigned char *)s) > 3) ||
2729 (*c >= 0x10000 && *c <= 0x1fffff && (p-(unsigned char *)s) > 4))
2730 code |= UTF8_CODE_OVERLONG;
2731
2732 /* Check invalid UTF8 range. */
2733 if ((*c >= 0xd800 && *c <= 0xdfff) ||
2734 (*c >= 0xfffe && *c <= 0xffff))
2735 code |= UTF8_CODE_INVRANGE;
2736
2737 return code | ((p-(unsigned char *)s)&0x0f);
2738}
2739
Willy Tarreaubaaee002006-06-26 02:48:02 +02002740/*
2741 * Local variables:
2742 * c-indent-level: 8
2743 * c-basic-offset: 8
2744 * End:
2745 */