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