blob: f28825f827acba11f2e392189c88cfd770deecf4 [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.
570 * The IPv6 '::' address is IN6ADDR_ANY.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200571 */
Willy Tarreau24709282013-03-10 21:32:12 +0100572static struct sockaddr_storage *str2ip(const char *str, struct sockaddr_storage *sa)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200573{
Willy Tarreaufab5a432011-03-04 15:31:53 +0100574 struct hostent *he;
575
Willy Tarreaufab5a432011-03-04 15:31:53 +0100576 /* Any IPv6 address */
577 if (str[0] == ':' && str[1] == ':' && !str[2]) {
Willy Tarreau24709282013-03-10 21:32:12 +0100578 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
579 sa->ss_family = AF_INET6;
580 else if (sa->ss_family != AF_INET6)
581 goto fail;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100582 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100583 }
584
Willy Tarreau24709282013-03-10 21:32:12 +0100585 /* Any address for the family, defaults to IPv4 */
Willy Tarreaufab5a432011-03-04 15:31:53 +0100586 if (!str[0] || (str[0] == '*' && !str[1])) {
Willy Tarreau24709282013-03-10 21:32:12 +0100587 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
588 sa->ss_family = AF_INET;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100589 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100590 }
591
592 /* check for IPv6 first */
Willy Tarreau24709282013-03-10 21:32:12 +0100593 if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET6) &&
594 inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)sa)->sin6_addr)) {
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100595 sa->ss_family = AF_INET6;
596 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100597 }
598
599 /* then check for IPv4 */
Willy Tarreau24709282013-03-10 21:32:12 +0100600 if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET) &&
601 inet_pton(AF_INET, str, &((struct sockaddr_in *)sa)->sin_addr)) {
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100602 sa->ss_family = AF_INET;
603 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100604 }
605
David du Colombierd5f43282011-03-17 10:40:16 +0100606#ifdef USE_GETADDRINFO
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200607 if (global.tune.options & GTUNE_USE_GAI) {
David du Colombierd5f43282011-03-17 10:40:16 +0100608 struct addrinfo hints, *result;
609
610 memset(&result, 0, sizeof(result));
611 memset(&hints, 0, sizeof(hints));
Willy Tarreau24709282013-03-10 21:32:12 +0100612 hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC;
David du Colombierd5f43282011-03-17 10:40:16 +0100613 hints.ai_socktype = SOCK_DGRAM;
614 hints.ai_flags = AI_PASSIVE;
615 hints.ai_protocol = 0;
616
617 if (getaddrinfo(str, NULL, &hints, &result) == 0) {
Willy Tarreau24709282013-03-10 21:32:12 +0100618 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
619 sa->ss_family = result->ai_family;
620 else if (sa->ss_family != result->ai_family)
621 goto fail;
622
David du Colombierd5f43282011-03-17 10:40:16 +0100623 switch (result->ai_family) {
624 case AF_INET:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100625 memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen);
626 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100627 case AF_INET6:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100628 memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen);
629 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100630 }
631 }
632
Sean Carey58ea0392013-02-15 23:39:18 +0100633 if (result)
634 freeaddrinfo(result);
Willy Tarreaufab5a432011-03-04 15:31:53 +0100635 }
David du Colombierd5f43282011-03-17 10:40:16 +0100636#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200637 /* try to resolve an IPv4/IPv6 hostname */
638 he = gethostbyname(str);
639 if (he) {
640 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
641 sa->ss_family = he->h_addrtype;
642 else if (sa->ss_family != he->h_addrtype)
643 goto fail;
644
645 switch (sa->ss_family) {
646 case AF_INET:
647 ((struct sockaddr_in *)sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
648 return sa;
649 case AF_INET6:
650 ((struct sockaddr_in6 *)sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
651 return sa;
652 }
653 }
654
David du Colombierd5f43282011-03-17 10:40:16 +0100655 /* unsupported address family */
Willy Tarreau24709282013-03-10 21:32:12 +0100656 fail:
Willy Tarreaufab5a432011-03-04 15:31:53 +0100657 return NULL;
658}
659
660/*
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100661 * Converts <str> to a locally allocated struct sockaddr_storage *, and a port
662 * range or offset consisting in two integers that the caller will have to
663 * check to find the relevant input format. The following format are supported :
664 *
665 * String format | address | port | low | high
666 * addr | <addr> | 0 | 0 | 0
667 * addr: | <addr> | 0 | 0 | 0
668 * addr:port | <addr> | <port> | <port> | <port>
669 * addr:pl-ph | <addr> | <pl> | <pl> | <ph>
670 * addr:+port | <addr> | <port> | 0 | <port>
671 * addr:-port | <addr> |-<port> | <port> | 0
672 *
673 * The detection of a port range or increment by the caller is made by
674 * comparing <low> and <high>. If both are equal, then port 0 means no port
675 * was specified. The caller may pass NULL for <low> and <high> if it is not
676 * interested in retrieving port ranges.
677 *
678 * Note that <addr> above may also be :
679 * - empty ("") => family will be AF_INET and address will be INADDR_ANY
680 * - "*" => family will be AF_INET and address will be INADDR_ANY
681 * - "::" => family will be AF_INET6 and address will be IN6ADDR_ANY
682 * - a host name => family and address will depend on host name resolving.
683 *
Willy Tarreau24709282013-03-10 21:32:12 +0100684 * A prefix may be passed in before the address above to force the family :
685 * - "ipv4@" => force address to resolve as IPv4 and fail if not possible.
686 * - "ipv6@" => force address to resolve as IPv6 and fail if not possible.
687 * - "unix@" => force address to be a path to a UNIX socket even if the
688 * path does not start with a '/'
Willy Tarreauccfccef2014-05-10 01:49:15 +0200689 * - 'abns@' -> force address to belong to the abstract namespace (Linux
690 * only). These sockets are just like Unix sockets but without
691 * the need for an underlying file system. The address is a
692 * string. Technically it's like a Unix socket with a zero in
693 * the first byte of the address.
Willy Tarreau40aa0702013-03-10 23:51:38 +0100694 * - "fd@" => an integer must follow, and is a file descriptor number.
Willy Tarreau24709282013-03-10 21:32:12 +0100695 *
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100696 * Also note that in order to avoid any ambiguity with IPv6 addresses, the ':'
697 * is mandatory after the IP address even when no port is specified. NULL is
698 * returned if the address cannot be parsed. The <low> and <high> ports are
Willy Tarreau24709282013-03-10 21:32:12 +0100699 * always initialized if non-null, even for non-IP families.
Willy Tarreaud393a622013-03-04 18:22:00 +0100700 *
701 * If <pfx> is non-null, it is used as a string prefix before any path-based
702 * address (typically the path to a unix socket).
Willy Tarreau40aa0702013-03-10 23:51:38 +0100703 *
704 * When a file descriptor is passed, its value is put into the s_addr part of
705 * the address when cast to sockaddr_in and the address family is AF_UNSPEC.
Willy Tarreaufab5a432011-03-04 15:31:53 +0100706 */
Willy Tarreaud393a622013-03-04 18:22:00 +0100707struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high, char **err, const char *pfx)
Willy Tarreaufab5a432011-03-04 15:31:53 +0100708{
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100709 static struct sockaddr_storage ss;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100710 struct sockaddr_storage *ret = NULL;
Willy Tarreau24709282013-03-10 21:32:12 +0100711 char *back, *str2;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100712 char *port1, *port2;
713 int portl, porth, porta;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200714 int abstract = 0;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100715
716 portl = porth = porta = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200717
Willy Tarreaudad36a32013-03-11 01:20:04 +0100718 str2 = back = env_expand(strdup(str));
Willy Tarreaudf350f12013-03-01 20:22:54 +0100719 if (str2 == NULL) {
720 memprintf(err, "out of memory in '%s'\n", __FUNCTION__);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100721 goto out;
Willy Tarreaudf350f12013-03-01 20:22:54 +0100722 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200723
Willy Tarreau24709282013-03-10 21:32:12 +0100724 memset(&ss, 0, sizeof(ss));
725
726 if (strncmp(str2, "unix@", 5) == 0) {
727 str2 += 5;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200728 abstract = 0;
Willy Tarreau24709282013-03-10 21:32:12 +0100729 ss.ss_family = AF_UNIX;
730 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200731 else if (strncmp(str2, "abns@", 5) == 0) {
732 str2 += 5;
733 abstract = 1;
734 ss.ss_family = AF_UNIX;
735 }
Willy Tarreau24709282013-03-10 21:32:12 +0100736 else if (strncmp(str2, "ipv4@", 5) == 0) {
737 str2 += 5;
738 ss.ss_family = AF_INET;
739 }
740 else if (strncmp(str2, "ipv6@", 5) == 0) {
741 str2 += 5;
742 ss.ss_family = AF_INET6;
743 }
744 else if (*str2 == '/') {
745 ss.ss_family = AF_UNIX;
746 }
747 else
748 ss.ss_family = AF_UNSPEC;
749
Willy Tarreau40aa0702013-03-10 23:51:38 +0100750 if (ss.ss_family == AF_UNSPEC && strncmp(str2, "fd@", 3) == 0) {
751 char *endptr;
752
753 str2 += 3;
754 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = strtol(str2, &endptr, 10);
755
756 if (!*str2 || *endptr) {
Willy Tarreaudad36a32013-03-11 01:20:04 +0100757 memprintf(err, "file descriptor '%s' is not a valid integer in '%s'\n", str2, str);
Willy Tarreau40aa0702013-03-10 23:51:38 +0100758 goto out;
759 }
760
761 /* we return AF_UNSPEC if we use a file descriptor number */
762 ss.ss_family = AF_UNSPEC;
763 }
764 else if (ss.ss_family == AF_UNIX) {
Willy Tarreau15586382013-03-04 19:48:14 +0100765 int prefix_path_len;
766 int max_path_len;
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200767 int adr_len;
Willy Tarreau15586382013-03-04 19:48:14 +0100768
769 /* complete unix socket path name during startup or soft-restart is
770 * <unix_bind_prefix><path>.<pid>.<bak|tmp>
771 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200772 prefix_path_len = (pfx && !abstract) ? strlen(pfx) : 0;
Willy Tarreau15586382013-03-04 19:48:14 +0100773 max_path_len = (sizeof(((struct sockaddr_un *)&ss)->sun_path) - 1) -
774 (prefix_path_len ? prefix_path_len + 1 + 5 + 1 + 3 : 0);
775
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200776 adr_len = strlen(str2);
777 if (adr_len > max_path_len) {
Willy Tarreau15586382013-03-04 19:48:14 +0100778 memprintf(err, "socket path '%s' too long (max %d)\n", str, max_path_len);
779 goto out;
780 }
781
Willy Tarreauccfccef2014-05-10 01:49:15 +0200782 /* when abstract==1, we skip the first zero and copy all bytes except the trailing zero */
783 memset(((struct sockaddr_un *)&ss)->sun_path, 0, sizeof(((struct sockaddr_un *)&ss)->sun_path));
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200784 if (prefix_path_len)
Willy Tarreau15586382013-03-04 19:48:14 +0100785 memcpy(((struct sockaddr_un *)&ss)->sun_path, pfx, prefix_path_len);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200786 memcpy(((struct sockaddr_un *)&ss)->sun_path + prefix_path_len + abstract, str2, adr_len + 1 - abstract);
Willy Tarreau15586382013-03-04 19:48:14 +0100787 }
Willy Tarreau24709282013-03-10 21:32:12 +0100788 else { /* IPv4 and IPv6 */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100789 port1 = strrchr(str2, ':');
790 if (port1)
791 *port1++ = '\0';
792 else
793 port1 = "";
Willy Tarreaubaaee002006-06-26 02:48:02 +0200794
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100795 if (str2ip(str2, &ss) == NULL) {
796 memprintf(err, "invalid address: '%s' in '%s'\n", str2, str);
797 goto out;
798 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100799
Willy Tarreaua39d1992013-04-01 20:37:42 +0200800 if (isdigit((int)(unsigned char)*port1)) { /* single port or range */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100801 port2 = strchr(port1, '-');
802 if (port2)
803 *port2++ = '\0';
804 else
805 port2 = port1;
806 portl = atoi(port1);
807 porth = atoi(port2);
808 porta = portl;
809 }
810 else if (*port1 == '-') { /* negative offset */
811 portl = atoi(port1 + 1);
812 porta = -portl;
813 }
814 else if (*port1 == '+') { /* positive offset */
815 porth = atoi(port1 + 1);
816 porta = porth;
817 }
818 else if (*port1) { /* other any unexpected char */
Willy Tarreaudad36a32013-03-11 01:20:04 +0100819 memprintf(err, "invalid character '%c' in port number '%s' in '%s'\n", *port1, port1, str);
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100820 goto out;
821 }
822 set_host_port(&ss, porta);
Willy Tarreaue4c58c82013-03-06 15:28:17 +0100823 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100824
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100825 ret = &ss;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100826 out:
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100827 if (low)
828 *low = portl;
829 if (high)
830 *high = porth;
Willy Tarreau24709282013-03-10 21:32:12 +0100831 free(back);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100832 return ret;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200833}
834
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100835/* converts <str> to a struct in_addr containing a network mask. It can be
836 * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1
837 * if the conversion succeeds otherwise non-zero.
838 */
839int str2mask(const char *str, struct in_addr *mask)
840{
841 if (strchr(str, '.') != NULL) { /* dotted notation */
842 if (!inet_pton(AF_INET, str, mask))
843 return 0;
844 }
845 else { /* mask length */
846 char *err;
847 unsigned long len = strtol(str, &err, 10);
848
849 if (!*str || (err && *err) || (unsigned)len > 32)
850 return 0;
851 if (len)
852 mask->s_addr = htonl(~0UL << (32 - len));
853 else
854 mask->s_addr = 0;
855 }
856 return 1;
857}
858
Thierry FOURNIERb0504632013-12-14 15:39:02 +0100859/* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
860 * succeeds otherwise zero.
861 */
862int cidr2dotted(int cidr, struct in_addr *mask) {
863
864 if (cidr < 0 || cidr > 32)
865 return 0;
866
867 mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0;
868 return 1;
869}
870
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200871/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200872 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200873 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
874 * is optionnal and either in the dotted or CIDR notation.
875 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
876 */
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100877int str2net(const char *str, int resolve, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200878{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200879 __label__ out_free, out_err;
880 char *c, *s;
881 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200882
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200883 s = strdup(str);
884 if (!s)
885 return 0;
886
Willy Tarreaubaaee002006-06-26 02:48:02 +0200887 memset(mask, 0, sizeof(*mask));
888 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200889
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200890 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200891 *c++ = '\0';
892 /* c points to the mask */
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100893 if (!str2mask(c, mask))
894 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895 }
896 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100897 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200898 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200899 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200900 struct hostent *he;
901
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100902 if (!resolve)
903 goto out_err;
904
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200905 if ((he = gethostbyname(s)) == NULL) {
906 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200907 }
908 else
909 *addr = *(struct in_addr *) *(he->h_addr_list);
910 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200911
912 ret_val = 1;
913 out_free:
914 free(s);
915 return ret_val;
916 out_err:
917 ret_val = 0;
918 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200919}
920
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100921
922/*
Willy Tarreau6d20e282012-04-27 22:49:47 +0200923 * converts <str> to two struct in6_addr* which must be pre-allocated.
924 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
925 * is an optionnal number of bits (128 being the default).
926 * Returns 1 if OK, 0 if error.
927 */
928int str62net(const char *str, struct in6_addr *addr, unsigned char *mask)
929{
930 char *c, *s;
931 int ret_val = 0;
932 char *err;
933 unsigned long len = 128;
934
935 s = strdup(str);
936 if (!s)
937 return 0;
938
939 memset(mask, 0, sizeof(*mask));
940 memset(addr, 0, sizeof(*addr));
941
942 if ((c = strrchr(s, '/')) != NULL) {
943 *c++ = '\0'; /* c points to the mask */
944 if (!*c)
945 goto out_free;
946
947 len = strtoul(c, &err, 10);
948 if ((err && *err) || (unsigned)len > 128)
949 goto out_free;
950 }
951 *mask = len; /* OK we have a valid mask in <len> */
952
953 if (!inet_pton(AF_INET6, s, addr))
954 goto out_free;
955
956 ret_val = 1;
957 out_free:
958 free(s);
959 return ret_val;
960}
961
962
963/*
David du Colombier6f5ccb12011-03-10 22:26:24 +0100964 * Parse IPv4 address found in url.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100965 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100966int url2ipv4(const char *addr, struct in_addr *dst)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100967{
968 int saw_digit, octets, ch;
969 u_char tmp[4], *tp;
970 const char *cp = addr;
971
972 saw_digit = 0;
973 octets = 0;
974 *(tp = tmp) = 0;
975
976 while (*addr) {
977 unsigned char digit = (ch = *addr++) - '0';
978 if (digit > 9 && ch != '.')
979 break;
980 if (digit <= 9) {
981 u_int new = *tp * 10 + digit;
982 if (new > 255)
983 return 0;
984 *tp = new;
985 if (!saw_digit) {
986 if (++octets > 4)
987 return 0;
988 saw_digit = 1;
989 }
990 } else if (ch == '.' && saw_digit) {
991 if (octets == 4)
992 return 0;
993 *++tp = 0;
994 saw_digit = 0;
995 } else
996 return 0;
997 }
998
999 if (octets < 4)
1000 return 0;
1001
1002 memcpy(&dst->s_addr, tmp, 4);
1003 return addr-cp-1;
1004}
1005
1006/*
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001007 * Resolve destination server from URL. Convert <str> to a sockaddr_storage.
1008 * <out> contain the code of the dectected scheme, the start and length of
1009 * the hostname. Actually only http and https are supported. <out> can be NULL.
1010 * This function returns the consumed length. It is useful if you parse complete
1011 * url like http://host:port/path, because the consumed length corresponds to
1012 * the first character of the path. If the conversion fails, it returns -1.
1013 *
1014 * This function tries to resolve the DNS name if haproxy is in starting mode.
1015 * So, this function may be used during the configuration parsing.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001016 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001017int url2sa(const char *url, int ulen, struct sockaddr_storage *addr, struct split_url *out)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001018{
1019 const char *curr = url, *cp = url;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001020 const char *end;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001021 int ret, url_code = 0;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001022 unsigned long long int http_code = 0;
1023 int default_port;
1024 struct hostent *he;
1025 char *p;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001026
1027 /* Firstly, try to find :// pattern */
1028 while (curr < url+ulen && url_code != 0x3a2f2f) {
1029 url_code = ((url_code & 0xffff) << 8);
1030 url_code += (unsigned char)*curr++;
1031 }
1032
1033 /* Secondly, if :// pattern is found, verify parsed stuff
1034 * before pattern is matching our http pattern.
1035 * If so parse ip address and port in uri.
1036 *
1037 * WARNING: Current code doesn't support dynamic async dns resolver.
1038 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001039 if (url_code != 0x3a2f2f)
1040 return -1;
1041
1042 /* Copy scheme, and utrn to lower case. */
1043 while (cp < curr - 3)
1044 http_code = (http_code << 8) + *cp++;
1045 http_code |= 0x2020202020202020ULL; /* Turn everything to lower case */
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001046
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001047 /* HTTP or HTTPS url matching */
1048 if (http_code == 0x2020202068747470ULL) {
1049 default_port = 80;
1050 if (out)
1051 out->scheme = SCH_HTTP;
1052 }
1053 else if (http_code == 0x2020206874747073ULL) {
1054 default_port = 443;
1055 if (out)
1056 out->scheme = SCH_HTTPS;
1057 }
1058 else
1059 return -1;
1060
1061 /* If the next char is '[', the host address is IPv6. */
1062 if (*curr == '[') {
1063 curr++;
1064
1065 /* Check trash size */
1066 if (trash.size < ulen)
1067 return -1;
1068
1069 /* Look for ']' and copy the address in a trash buffer. */
1070 p = trash.str;
1071 for (end = curr;
1072 end < url + ulen && *end != ']';
1073 end++, p++)
1074 *p = *end;
1075 if (*end != ']')
1076 return -1;
1077 *p = '\0';
1078
1079 /* Update out. */
1080 if (out) {
1081 out->host = curr;
1082 out->host_len = end - curr;
1083 }
1084
1085 /* Try IPv6 decoding. */
1086 if (!inet_pton(AF_INET6, trash.str, &((struct sockaddr_in6 *)addr)->sin6_addr))
1087 return -1;
1088 end++;
1089
1090 /* Decode port. */
1091 if (*end == ':') {
1092 end++;
1093 default_port = read_uint(&end, url + ulen);
1094 }
1095 ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port);
1096 ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
1097 return end - url;
1098 }
1099 else {
1100 /* We are looking for IP address. If you want to parse and
1101 * resolve hostname found in url, you can use str2sa_range(), but
1102 * be warned this can slow down global daemon performances
1103 * while handling lagging dns responses.
1104 */
1105 ret = url2ipv4(curr, &((struct sockaddr_in *)addr)->sin_addr);
1106 if (ret) {
1107 /* Update out. */
1108 if (out) {
1109 out->host = curr;
1110 out->host_len = ret;
1111 }
1112
1113 curr += ret;
1114
1115 /* Decode port. */
1116 if (*curr == ':') {
1117 curr++;
1118 default_port = read_uint(&curr, url + ulen);
1119 }
1120 ((struct sockaddr_in *)addr)->sin_port = htons(default_port);
1121
1122 /* Set family. */
1123 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
1124 return curr - url;
1125 }
1126 else if (global.mode & MODE_STARTING) {
1127 /* The IPv4 and IPv6 decoding fails, maybe the url contain name. Try to execute
1128 * synchronous DNS request only if HAProxy is in the start state.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001129 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001130
1131 /* look for : or / or end */
1132 for (end = curr;
1133 end < url + ulen && *end != '/' && *end != ':';
1134 end++);
1135 memcpy(trash.str, curr, end - curr);
1136 trash.str[end - curr] = '\0';
1137
1138 /* try to resolve an IPv4/IPv6 hostname */
1139 he = gethostbyname(trash.str);
1140 if (!he)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001141 return -1;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001142
1143 /* Update out. */
1144 if (out) {
1145 out->host = curr;
1146 out->host_len = end - curr;
1147 }
1148
1149 /* Decode port. */
1150 if (*end == ':') {
1151 end++;
1152 default_port = read_uint(&end, url + ulen);
1153 }
1154
1155 /* Copy IP address, set port and family. */
1156 switch (he->h_addrtype) {
1157 case AF_INET:
1158 ((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
1159 ((struct sockaddr_in *)addr)->sin_port = htons(default_port);
1160 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
1161 return end - url;
1162
1163 case AF_INET6:
1164 ((struct sockaddr_in6 *)addr)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
1165 ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port);
1166 ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
1167 return end - url;
1168 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001169 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001170 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001171 return -1;
1172}
1173
Willy Tarreau631f01c2011-09-05 00:36:48 +02001174/* Tries to convert a sockaddr_storage address to text form. Upon success, the
1175 * address family is returned so that it's easy for the caller to adapt to the
1176 * output format. Zero is returned if the address family is not supported. -1
1177 * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
1178 * supported.
1179 */
1180int addr_to_str(struct sockaddr_storage *addr, char *str, int size)
1181{
1182
1183 void *ptr;
1184
1185 if (size < 5)
1186 return 0;
1187 *str = '\0';
1188
1189 switch (addr->ss_family) {
1190 case AF_INET:
1191 ptr = &((struct sockaddr_in *)addr)->sin_addr;
1192 break;
1193 case AF_INET6:
1194 ptr = &((struct sockaddr_in6 *)addr)->sin6_addr;
1195 break;
1196 case AF_UNIX:
1197 memcpy(str, "unix", 5);
1198 return addr->ss_family;
1199 default:
1200 return 0;
1201 }
1202
1203 if (inet_ntop(addr->ss_family, ptr, str, size))
1204 return addr->ss_family;
1205
1206 /* failed */
1207 return -1;
1208}
1209
Simon Horman75ab8bd2014-06-16 09:39:41 +09001210/* Tries to convert a sockaddr_storage port to text form. Upon success, the
1211 * address family is returned so that it's easy for the caller to adapt to the
1212 * output format. Zero is returned if the address family is not supported. -1
1213 * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
1214 * supported.
1215 */
1216int port_to_str(struct sockaddr_storage *addr, char *str, int size)
1217{
1218
1219 uint16_t port;
1220
1221
1222 if (size < 5)
1223 return 0;
1224 *str = '\0';
1225
1226 switch (addr->ss_family) {
1227 case AF_INET:
1228 port = ((struct sockaddr_in *)addr)->sin_port;
1229 break;
1230 case AF_INET6:
1231 port = ((struct sockaddr_in6 *)addr)->sin6_port;
1232 break;
1233 case AF_UNIX:
1234 memcpy(str, "unix", 5);
1235 return addr->ss_family;
1236 default:
1237 return 0;
1238 }
1239
1240 snprintf(str, size, "%u", ntohs(port));
1241 return addr->ss_family;
1242}
1243
Willy Tarreaubaaee002006-06-26 02:48:02 +02001244/* will try to encode the string <string> replacing all characters tagged in
1245 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
1246 * prefixed by <escape>, and will store the result between <start> (included)
1247 * and <stop> (excluded), and will always terminate the string with a '\0'
1248 * before <stop>. The position of the '\0' is returned if the conversion
1249 * completes. If bytes are missing between <start> and <stop>, then the
1250 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
1251 * cannot even be stored so we return <start> without writing the 0.
1252 * The input string must also be zero-terminated.
1253 */
1254const char hextab[16] = "0123456789ABCDEF";
1255char *encode_string(char *start, char *stop,
1256 const char escape, const fd_set *map,
1257 const char *string)
1258{
1259 if (start < stop) {
1260 stop--; /* reserve one byte for the final '\0' */
1261 while (start < stop && *string != '\0') {
1262 if (!FD_ISSET((unsigned char)(*string), map))
1263 *start++ = *string;
1264 else {
1265 if (start + 3 >= stop)
1266 break;
1267 *start++ = escape;
1268 *start++ = hextab[(*string >> 4) & 15];
1269 *start++ = hextab[*string & 15];
1270 }
1271 string++;
1272 }
1273 *start = '\0';
1274 }
1275 return start;
1276}
1277
Thierry FOURNIERe059ec92014-03-17 12:01:13 +01001278/*
1279 * Same behavior as encode_string() above, except that it encodes chunk
1280 * <chunk> instead of a string.
1281 */
1282char *encode_chunk(char *start, char *stop,
1283 const char escape, const fd_set *map,
1284 const struct chunk *chunk)
1285{
1286 char *str = chunk->str;
1287 char *end = chunk->str + chunk->len;
1288
1289 if (start < stop) {
1290 stop--; /* reserve one byte for the final '\0' */
1291 while (start < stop && str < end) {
1292 if (!FD_ISSET((unsigned char)(*str), map))
1293 *start++ = *str;
1294 else {
1295 if (start + 3 >= stop)
1296 break;
1297 *start++ = escape;
1298 *start++ = hextab[(*str >> 4) & 15];
1299 *start++ = hextab[*str & 15];
1300 }
1301 str++;
1302 }
1303 *start = '\0';
1304 }
1305 return start;
1306}
1307
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001308/* Decode an URL-encoded string in-place. The resulting string might
1309 * be shorter. If some forbidden characters are found, the conversion is
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001310 * aborted, the string is truncated before the issue and a negative value is
1311 * returned, otherwise the operation returns the length of the decoded string.
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001312 */
1313int url_decode(char *string)
1314{
1315 char *in, *out;
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001316 int ret = -1;
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001317
1318 in = string;
1319 out = string;
1320 while (*in) {
1321 switch (*in) {
1322 case '+' :
1323 *out++ = ' ';
1324 break;
1325 case '%' :
1326 if (!ishex(in[1]) || !ishex(in[2]))
1327 goto end;
1328 *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]);
1329 in += 2;
1330 break;
1331 default:
1332 *out++ = *in;
1333 break;
1334 }
1335 in++;
1336 }
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001337 ret = out - string; /* success */
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001338 end:
1339 *out = 0;
1340 return ret;
1341}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001342
Willy Tarreau6911fa42007-03-04 18:06:08 +01001343unsigned int str2ui(const char *s)
1344{
1345 return __str2ui(s);
1346}
1347
1348unsigned int str2uic(const char *s)
1349{
1350 return __str2uic(s);
1351}
1352
1353unsigned int strl2ui(const char *s, int len)
1354{
1355 return __strl2ui(s, len);
1356}
1357
1358unsigned int strl2uic(const char *s, int len)
1359{
1360 return __strl2uic(s, len);
1361}
1362
Willy Tarreau4ec83cd2010-10-15 23:19:55 +02001363unsigned int read_uint(const char **s, const char *end)
1364{
1365 return __read_uint(s, end);
1366}
1367
Willy Tarreau6911fa42007-03-04 18:06:08 +01001368/* This one is 7 times faster than strtol() on athlon with checks.
1369 * It returns the value of the number composed of all valid digits read,
1370 * and can process negative numbers too.
1371 */
1372int strl2ic(const char *s, int len)
1373{
1374 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001375 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001376
1377 if (len > 0) {
1378 if (*s != '-') {
1379 /* positive number */
1380 while (len-- > 0) {
1381 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001382 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001383 if (j > 9)
1384 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001385 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001386 }
1387 } else {
1388 /* negative number */
1389 s++;
1390 while (--len > 0) {
1391 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001392 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001393 if (j > 9)
1394 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001395 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001396 }
1397 }
1398 }
1399 return i;
1400}
1401
1402
1403/* This function reads exactly <len> chars from <s> and converts them to a
1404 * signed integer which it stores into <ret>. It accurately detects any error
1405 * (truncated string, invalid chars, overflows). It is meant to be used in
1406 * applications designed for hostile environments. It returns zero when the
1407 * number has successfully been converted, non-zero otherwise. When an error
1408 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
1409 * faster than strtol().
1410 */
1411int strl2irc(const char *s, int len, int *ret)
1412{
1413 int i = 0;
1414 int j;
1415
1416 if (!len)
1417 return 1;
1418
1419 if (*s != '-') {
1420 /* positive number */
1421 while (len-- > 0) {
1422 j = (*s++) - '0';
1423 if (j > 9) return 1; /* invalid char */
1424 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
1425 i = i * 10;
1426 if (i + j < i) return 1; /* check for addition overflow */
1427 i = i + j;
1428 }
1429 } else {
1430 /* negative number */
1431 s++;
1432 while (--len > 0) {
1433 j = (*s++) - '0';
1434 if (j > 9) return 1; /* invalid char */
1435 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
1436 i = i * 10;
1437 if (i - j > i) return 1; /* check for subtract overflow */
1438 i = i - j;
1439 }
1440 }
1441 *ret = i;
1442 return 0;
1443}
1444
1445
1446/* This function reads exactly <len> chars from <s> and converts them to a
1447 * signed integer which it stores into <ret>. It accurately detects any error
1448 * (truncated string, invalid chars, overflows). It is meant to be used in
1449 * applications designed for hostile environments. It returns zero when the
1450 * number has successfully been converted, non-zero otherwise. When an error
1451 * is returned, the <ret> value is left untouched. It is about 3 times slower
1452 * than str2irc().
1453 */
Willy Tarreau6911fa42007-03-04 18:06:08 +01001454
1455int strl2llrc(const char *s, int len, long long *ret)
1456{
1457 long long i = 0;
1458 int j;
1459
1460 if (!len)
1461 return 1;
1462
1463 if (*s != '-') {
1464 /* positive number */
1465 while (len-- > 0) {
1466 j = (*s++) - '0';
1467 if (j > 9) return 1; /* invalid char */
1468 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
1469 i = i * 10LL;
1470 if (i + j < i) return 1; /* check for addition overflow */
1471 i = i + j;
1472 }
1473 } else {
1474 /* negative number */
1475 s++;
1476 while (--len > 0) {
1477 j = (*s++) - '0';
1478 if (j > 9) return 1; /* invalid char */
1479 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
1480 i = i * 10LL;
1481 if (i - j > i) return 1; /* check for subtract overflow */
1482 i = i - j;
1483 }
1484 }
1485 *ret = i;
1486 return 0;
1487}
1488
Thierry FOURNIER511e9472014-01-23 17:40:34 +01001489/* This function is used with pat_parse_dotted_ver(). It converts a string
1490 * composed by two number separated by a dot. Each part must contain in 16 bits
1491 * because internally they will be represented as a 32-bit quantity stored in
1492 * a 64-bit integer. It returns zero when the number has successfully been
1493 * converted, non-zero otherwise. When an error is returned, the <ret> value
1494 * is left untouched.
1495 *
1496 * "1.3" -> 0x0000000000010003
1497 * "65535.65535" -> 0x00000000ffffffff
1498 */
1499int strl2llrc_dotted(const char *text, int len, long long *ret)
1500{
1501 const char *end = &text[len];
1502 const char *p;
1503 long long major, minor;
1504
1505 /* Look for dot. */
1506 for (p = text; p < end; p++)
1507 if (*p == '.')
1508 break;
1509
1510 /* Convert major. */
1511 if (strl2llrc(text, p - text, &major) != 0)
1512 return 1;
1513
1514 /* Check major. */
1515 if (major >= 65536)
1516 return 1;
1517
1518 /* Convert minor. */
1519 minor = 0;
1520 if (p < end)
1521 if (strl2llrc(p + 1, end - (p + 1), &minor) != 0)
1522 return 1;
1523
1524 /* Check minor. */
1525 if (minor >= 65536)
1526 return 1;
1527
1528 /* Compose value. */
1529 *ret = (major << 16) | (minor & 0xffff);
1530 return 0;
1531}
1532
Willy Tarreaua0d37b62007-12-02 22:00:35 +01001533/* This function parses a time value optionally followed by a unit suffix among
1534 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
1535 * expected by the caller. The computation does its best to avoid overflows.
1536 * The value is returned in <ret> if everything is fine, and a NULL is returned
1537 * by the function. In case of error, a pointer to the error is returned and
1538 * <ret> is left untouched. Values are automatically rounded up when needed.
1539 */
1540const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
1541{
1542 unsigned imult, idiv;
1543 unsigned omult, odiv;
1544 unsigned value;
1545
1546 omult = odiv = 1;
1547
1548 switch (unit_flags & TIME_UNIT_MASK) {
1549 case TIME_UNIT_US: omult = 1000000; break;
1550 case TIME_UNIT_MS: omult = 1000; break;
1551 case TIME_UNIT_S: break;
1552 case TIME_UNIT_MIN: odiv = 60; break;
1553 case TIME_UNIT_HOUR: odiv = 3600; break;
1554 case TIME_UNIT_DAY: odiv = 86400; break;
1555 default: break;
1556 }
1557
1558 value = 0;
1559
1560 while (1) {
1561 unsigned int j;
1562
1563 j = *text - '0';
1564 if (j > 9)
1565 break;
1566 text++;
1567 value *= 10;
1568 value += j;
1569 }
1570
1571 imult = idiv = 1;
1572 switch (*text) {
1573 case '\0': /* no unit = default unit */
1574 imult = omult = idiv = odiv = 1;
1575 break;
1576 case 's': /* second = unscaled unit */
1577 break;
1578 case 'u': /* microsecond : "us" */
1579 if (text[1] == 's') {
1580 idiv = 1000000;
1581 text++;
1582 }
1583 break;
1584 case 'm': /* millisecond : "ms" or minute: "m" */
1585 if (text[1] == 's') {
1586 idiv = 1000;
1587 text++;
1588 } else
1589 imult = 60;
1590 break;
1591 case 'h': /* hour : "h" */
1592 imult = 3600;
1593 break;
1594 case 'd': /* day : "d" */
1595 imult = 86400;
1596 break;
1597 default:
1598 return text;
1599 break;
1600 }
1601
1602 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
1603 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
1604 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
1605 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
1606
1607 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
1608 *ret = value;
1609 return NULL;
1610}
Willy Tarreau6911fa42007-03-04 18:06:08 +01001611
Emeric Brun39132b22010-01-04 14:57:24 +01001612/* this function converts the string starting at <text> to an unsigned int
1613 * stored in <ret>. If an error is detected, the pointer to the unexpected
1614 * character is returned. If the conversio is succesful, NULL is returned.
1615 */
1616const char *parse_size_err(const char *text, unsigned *ret) {
1617 unsigned value = 0;
1618
1619 while (1) {
1620 unsigned int j;
1621
1622 j = *text - '0';
1623 if (j > 9)
1624 break;
1625 if (value > ~0U / 10)
1626 return text;
1627 value *= 10;
1628 if (value > (value + j))
1629 return text;
1630 value += j;
1631 text++;
1632 }
1633
1634 switch (*text) {
1635 case '\0':
1636 break;
1637 case 'K':
1638 case 'k':
1639 if (value > ~0U >> 10)
1640 return text;
1641 value = value << 10;
1642 break;
1643 case 'M':
1644 case 'm':
1645 if (value > ~0U >> 20)
1646 return text;
1647 value = value << 20;
1648 break;
1649 case 'G':
1650 case 'g':
1651 if (value > ~0U >> 30)
1652 return text;
1653 value = value << 30;
1654 break;
1655 default:
1656 return text;
1657 }
1658
Godbach58048a22015-01-28 17:36:16 +08001659 if (*text != '\0' && *++text != '\0')
1660 return text;
1661
Emeric Brun39132b22010-01-04 14:57:24 +01001662 *ret = value;
1663 return NULL;
1664}
1665
Willy Tarreau126d4062013-12-03 17:50:47 +01001666/*
1667 * Parse binary string written in hexadecimal (source) and store the decoded
1668 * result into binstr and set binstrlen to the lengh of binstr. Memory for
1669 * binstr is allocated by the function. In case of error, returns 0 with an
Thierry FOURNIERee330af2014-01-21 11:36:14 +01001670 * error message in err. In succes case, it returns the consumed length.
Willy Tarreau126d4062013-12-03 17:50:47 +01001671 */
1672int parse_binary(const char *source, char **binstr, int *binstrlen, char **err)
1673{
1674 int len;
1675 const char *p = source;
1676 int i,j;
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001677 int alloc;
Willy Tarreau126d4062013-12-03 17:50:47 +01001678
1679 len = strlen(source);
1680 if (len % 2) {
1681 memprintf(err, "an even number of hex digit is expected");
1682 return 0;
1683 }
1684
1685 len = len >> 1;
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001686
Willy Tarreau126d4062013-12-03 17:50:47 +01001687 if (!*binstr) {
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001688 *binstr = calloc(len, sizeof(char));
1689 if (!*binstr) {
1690 memprintf(err, "out of memory while loading string pattern");
1691 return 0;
1692 }
1693 alloc = 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001694 }
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001695 else {
1696 if (*binstrlen < len) {
1697 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
1698 len, *binstrlen);
1699 return 0;
1700 }
1701 alloc = 0;
1702 }
1703 *binstrlen = len;
Willy Tarreau126d4062013-12-03 17:50:47 +01001704
1705 i = j = 0;
1706 while (j < len) {
1707 if (!ishex(p[i++]))
1708 goto bad_input;
1709 if (!ishex(p[i++]))
1710 goto bad_input;
1711 (*binstr)[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
1712 }
Thierry FOURNIERee330af2014-01-21 11:36:14 +01001713 return len << 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001714
1715bad_input:
1716 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001717 if (alloc)
1718 free(binstr);
Willy Tarreau126d4062013-12-03 17:50:47 +01001719 return 0;
1720}
1721
Willy Tarreau946ba592009-05-10 15:41:18 +02001722/* copies at most <n> characters from <src> and always terminates with '\0' */
1723char *my_strndup(const char *src, int n)
1724{
1725 int len = 0;
1726 char *ret;
1727
1728 while (len < n && src[len])
1729 len++;
1730
1731 ret = (char *)malloc(len + 1);
1732 if (!ret)
1733 return ret;
1734 memcpy(ret, src, len);
1735 ret[len] = '\0';
1736 return ret;
1737}
1738
Baptiste Assmannbb77c8e2013-10-06 23:24:13 +02001739/*
1740 * search needle in haystack
1741 * returns the pointer if found, returns NULL otherwise
1742 */
1743const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen)
1744{
1745 const void *c = NULL;
1746 unsigned char f;
1747
1748 if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen))
1749 return NULL;
1750
1751 f = *(char *)needle;
1752 c = haystack;
1753 while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) {
1754 if ((haystacklen - (c - haystack)) < needlelen)
1755 return NULL;
1756
1757 if (memcmp(c, needle, needlelen) == 0)
1758 return c;
1759 ++c;
1760 }
1761 return NULL;
1762}
1763
Willy Tarreau482b00d2009-10-04 22:48:42 +02001764/* This function returns the first unused key greater than or equal to <key> in
1765 * ID tree <root>. Zero is returned if no place is found.
1766 */
1767unsigned int get_next_id(struct eb_root *root, unsigned int key)
1768{
1769 struct eb32_node *used;
1770
1771 do {
1772 used = eb32_lookup_ge(root, key);
1773 if (!used || used->key > key)
1774 return key; /* key is available */
1775 key++;
1776 } while (key);
1777 return key;
1778}
1779
Willy Tarreau348238b2010-01-18 15:05:57 +01001780/* This function compares a sample word possibly followed by blanks to another
1781 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
1782 * otherwise zero. This intends to be used when checking HTTP headers for some
1783 * values. Note that it validates a word followed only by blanks but does not
1784 * validate a word followed by blanks then other chars.
1785 */
1786int word_match(const char *sample, int slen, const char *word, int wlen)
1787{
1788 if (slen < wlen)
1789 return 0;
1790
1791 while (wlen) {
1792 char c = *sample ^ *word;
1793 if (c && c != ('A' ^ 'a'))
1794 return 0;
1795 sample++;
1796 word++;
1797 slen--;
1798 wlen--;
1799 }
1800
1801 while (slen) {
1802 if (*sample != ' ' && *sample != '\t')
1803 return 0;
1804 sample++;
1805 slen--;
1806 }
1807 return 1;
1808}
Willy Tarreau482b00d2009-10-04 22:48:42 +02001809
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001810/* Converts any text-formatted IPv4 address to a host-order IPv4 address. It
1811 * is particularly fast because it avoids expensive operations such as
1812 * multiplies, which are optimized away at the end. It requires a properly
1813 * formated address though (3 points).
1814 */
1815unsigned int inetaddr_host(const char *text)
1816{
1817 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1818 register unsigned int dig100, dig10, dig1;
1819 int s;
1820 const char *p, *d;
1821
1822 dig1 = dig10 = dig100 = ascii_zero;
1823 s = 24;
1824
1825 p = text;
1826 while (1) {
1827 if (((unsigned)(*p - '0')) <= 9) {
1828 p++;
1829 continue;
1830 }
1831
1832 /* here, we have a complete byte between <text> and <p> (exclusive) */
1833 if (p == text)
1834 goto end;
1835
1836 d = p - 1;
1837 dig1 |= (unsigned int)(*d << s);
1838 if (d == text)
1839 goto end;
1840
1841 d--;
1842 dig10 |= (unsigned int)(*d << s);
1843 if (d == text)
1844 goto end;
1845
1846 d--;
1847 dig100 |= (unsigned int)(*d << s);
1848 end:
1849 if (!s || *p != '.')
1850 break;
1851
1852 s -= 8;
1853 text = ++p;
1854 }
1855
1856 dig100 -= ascii_zero;
1857 dig10 -= ascii_zero;
1858 dig1 -= ascii_zero;
1859 return ((dig100 * 10) + dig10) * 10 + dig1;
1860}
1861
1862/*
1863 * Idem except the first unparsed character has to be passed in <stop>.
1864 */
1865unsigned int inetaddr_host_lim(const char *text, const char *stop)
1866{
1867 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1868 register unsigned int dig100, dig10, dig1;
1869 int s;
1870 const char *p, *d;
1871
1872 dig1 = dig10 = dig100 = ascii_zero;
1873 s = 24;
1874
1875 p = text;
1876 while (1) {
1877 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1878 p++;
1879 continue;
1880 }
1881
1882 /* here, we have a complete byte between <text> and <p> (exclusive) */
1883 if (p == text)
1884 goto end;
1885
1886 d = p - 1;
1887 dig1 |= (unsigned int)(*d << s);
1888 if (d == text)
1889 goto end;
1890
1891 d--;
1892 dig10 |= (unsigned int)(*d << s);
1893 if (d == text)
1894 goto end;
1895
1896 d--;
1897 dig100 |= (unsigned int)(*d << s);
1898 end:
1899 if (!s || p == stop || *p != '.')
1900 break;
1901
1902 s -= 8;
1903 text = ++p;
1904 }
1905
1906 dig100 -= ascii_zero;
1907 dig10 -= ascii_zero;
1908 dig1 -= ascii_zero;
1909 return ((dig100 * 10) + dig10) * 10 + dig1;
1910}
1911
1912/*
1913 * Idem except the pointer to first unparsed byte is returned into <ret> which
1914 * must not be NULL.
1915 */
Willy Tarreau74172752010-10-15 23:21:42 +02001916unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret)
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001917{
1918 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1919 register unsigned int dig100, dig10, dig1;
1920 int s;
Willy Tarreau74172752010-10-15 23:21:42 +02001921 char *p, *d;
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001922
1923 dig1 = dig10 = dig100 = ascii_zero;
1924 s = 24;
1925
1926 p = text;
1927 while (1) {
1928 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1929 p++;
1930 continue;
1931 }
1932
1933 /* here, we have a complete byte between <text> and <p> (exclusive) */
1934 if (p == text)
1935 goto end;
1936
1937 d = p - 1;
1938 dig1 |= (unsigned int)(*d << s);
1939 if (d == text)
1940 goto end;
1941
1942 d--;
1943 dig10 |= (unsigned int)(*d << s);
1944 if (d == text)
1945 goto end;
1946
1947 d--;
1948 dig100 |= (unsigned int)(*d << s);
1949 end:
1950 if (!s || p == stop || *p != '.')
1951 break;
1952
1953 s -= 8;
1954 text = ++p;
1955 }
1956
1957 *ret = p;
1958 dig100 -= ascii_zero;
1959 dig10 -= ascii_zero;
1960 dig1 -= ascii_zero;
1961 return ((dig100 * 10) + dig10) * 10 + dig1;
1962}
1963
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001964/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
1965 * or the number of chars read in case of success. Maybe this could be replaced
1966 * by one of the functions above. Also, apparently this function does not support
1967 * hosts above 255 and requires exactly 4 octets.
Willy Tarreau075415a2013-12-12 11:29:39 +01001968 * The destination is only modified on success.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001969 */
1970int buf2ip(const char *buf, size_t len, struct in_addr *dst)
1971{
1972 const char *addr;
1973 int saw_digit, octets, ch;
1974 u_char tmp[4], *tp;
1975 const char *cp = buf;
1976
1977 saw_digit = 0;
1978 octets = 0;
1979 *(tp = tmp) = 0;
1980
1981 for (addr = buf; addr - buf < len; addr++) {
1982 unsigned char digit = (ch = *addr) - '0';
1983
1984 if (digit > 9 && ch != '.')
1985 break;
1986
1987 if (digit <= 9) {
1988 u_int new = *tp * 10 + digit;
1989
1990 if (new > 255)
1991 return 0;
1992
1993 *tp = new;
1994
1995 if (!saw_digit) {
1996 if (++octets > 4)
1997 return 0;
1998 saw_digit = 1;
1999 }
2000 } else if (ch == '.' && saw_digit) {
2001 if (octets == 4)
2002 return 0;
2003
2004 *++tp = 0;
2005 saw_digit = 0;
2006 } else
2007 return 0;
2008 }
2009
2010 if (octets < 4)
2011 return 0;
2012
2013 memcpy(&dst->s_addr, tmp, 4);
2014 return addr - cp;
2015}
2016
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002017/* This function converts the string in <buf> of the len <len> to
2018 * struct in6_addr <dst> which must be allocated by the caller.
2019 * This function returns 1 in success case, otherwise zero.
Willy Tarreau075415a2013-12-12 11:29:39 +01002020 * The destination is only modified on success.
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002021 */
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002022int buf2ip6(const char *buf, size_t len, struct in6_addr *dst)
2023{
Thierry FOURNIERcd659912013-12-11 12:33:54 +01002024 char null_term_ip6[INET6_ADDRSTRLEN + 1];
Willy Tarreau075415a2013-12-12 11:29:39 +01002025 struct in6_addr out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002026
Thierry FOURNIERcd659912013-12-11 12:33:54 +01002027 if (len > INET6_ADDRSTRLEN)
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002028 return 0;
2029
2030 memcpy(null_term_ip6, buf, len);
2031 null_term_ip6[len] = '\0';
2032
Willy Tarreau075415a2013-12-12 11:29:39 +01002033 if (!inet_pton(AF_INET6, null_term_ip6, &out))
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002034 return 0;
2035
Willy Tarreau075415a2013-12-12 11:29:39 +01002036 *dst = out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002037 return 1;
2038}
2039
Willy Tarreauacf95772010-06-14 19:09:21 +02002040/* To be used to quote config arg positions. Returns the short string at <ptr>
2041 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
2042 * if ptr is NULL or empty. The string is locally allocated.
2043 */
2044const char *quote_arg(const char *ptr)
2045{
2046 static char val[32];
2047 int i;
2048
2049 if (!ptr || !*ptr)
2050 return "end of line";
2051 val[0] = '\'';
Willy Tarreaude2dd6b2013-01-24 02:14:42 +01002052 for (i = 1; i < sizeof(val) - 2 && *ptr; i++)
Willy Tarreauacf95772010-06-14 19:09:21 +02002053 val[i] = *ptr++;
2054 val[i++] = '\'';
2055 val[i] = '\0';
2056 return val;
2057}
2058
Willy Tarreau5b180202010-07-18 10:40:48 +02002059/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
2060int get_std_op(const char *str)
2061{
2062 int ret = -1;
2063
2064 if (*str == 'e' && str[1] == 'q')
2065 ret = STD_OP_EQ;
2066 else if (*str == 'n' && str[1] == 'e')
2067 ret = STD_OP_NE;
2068 else if (*str == 'l') {
2069 if (str[1] == 'e') ret = STD_OP_LE;
2070 else if (str[1] == 't') ret = STD_OP_LT;
2071 }
2072 else if (*str == 'g') {
2073 if (str[1] == 'e') ret = STD_OP_GE;
2074 else if (str[1] == 't') ret = STD_OP_GT;
2075 }
2076
2077 if (ret == -1 || str[2] != '\0')
2078 return -1;
2079 return ret;
2080}
2081
Willy Tarreau4c14eaa2010-11-24 14:01:45 +01002082/* hash a 32-bit integer to another 32-bit integer */
2083unsigned int full_hash(unsigned int a)
2084{
2085 return __full_hash(a);
2086}
2087
David du Colombier4f92d322011-03-24 11:09:31 +01002088/* Return non-zero if IPv4 address is part of the network,
2089 * otherwise zero.
2090 */
2091int in_net_ipv4(struct in_addr *addr, struct in_addr *mask, struct in_addr *net)
2092{
2093 return((addr->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr));
2094}
2095
2096/* Return non-zero if IPv6 address is part of the network,
2097 * otherwise zero.
2098 */
2099int in_net_ipv6(struct in6_addr *addr, struct in6_addr *mask, struct in6_addr *net)
2100{
2101 int i;
2102
2103 for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++)
2104 if (((((int *)addr)[i] & ((int *)mask)[i])) !=
2105 (((int *)net)[i] & ((int *)mask)[i]))
2106 return 0;
2107 return 1;
2108}
2109
2110/* RFC 4291 prefix */
2111const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00,
2112 0x00, 0x00, 0x00, 0x00,
2113 0x00, 0x00, 0xFF, 0xFF };
2114
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002115/* Map IPv4 adress on IPv6 address, as specified in RFC 3513.
2116 * Input and output may overlap.
2117 */
David du Colombier4f92d322011-03-24 11:09:31 +01002118void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr)
2119{
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002120 struct in_addr tmp_addr;
2121
2122 tmp_addr.s_addr = sin_addr->s_addr;
David du Colombier4f92d322011-03-24 11:09:31 +01002123 memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx));
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002124 memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4);
David du Colombier4f92d322011-03-24 11:09:31 +01002125}
2126
2127/* Map IPv6 adress on IPv4 address, as specified in RFC 3513.
2128 * Return true if conversion is possible and false otherwise.
2129 */
2130int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr)
2131{
2132 if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) {
2133 memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]),
2134 sizeof(struct in_addr));
2135 return 1;
2136 }
2137
2138 return 0;
2139}
2140
William Lallemand421f5b52012-02-06 18:15:57 +01002141char *human_time(int t, short hz_div) {
2142 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
2143 char *p = rv;
Willy Tarreau761b3d52014-04-14 14:53:06 +02002144 char *end = rv + sizeof(rv);
William Lallemand421f5b52012-02-06 18:15:57 +01002145 int cnt=2; // print two numbers
2146
2147 if (unlikely(t < 0 || hz_div <= 0)) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002148 snprintf(p, end - p, "?");
William Lallemand421f5b52012-02-06 18:15:57 +01002149 return rv;
2150 }
2151
2152 if (unlikely(hz_div > 1))
2153 t /= hz_div;
2154
2155 if (t >= DAY) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002156 p += snprintf(p, end - p, "%dd", t / DAY);
William Lallemand421f5b52012-02-06 18:15:57 +01002157 cnt--;
2158 }
2159
2160 if (cnt && t % DAY / HOUR) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002161 p += snprintf(p, end - p, "%dh", t % DAY / HOUR);
William Lallemand421f5b52012-02-06 18:15:57 +01002162 cnt--;
2163 }
2164
2165 if (cnt && t % HOUR / MINUTE) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002166 p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE);
William Lallemand421f5b52012-02-06 18:15:57 +01002167 cnt--;
2168 }
2169
2170 if ((cnt && t % MINUTE) || !t) // also display '0s'
Willy Tarreau761b3d52014-04-14 14:53:06 +02002171 p += snprintf(p, end - p, "%ds", t % MINUTE / SEC);
William Lallemand421f5b52012-02-06 18:15:57 +01002172
2173 return rv;
2174}
2175
2176const char *monthname[12] = {
2177 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2178 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2179};
2180
2181/* date2str_log: write a date in the format :
2182 * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d",
2183 * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2184 * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000);
2185 *
2186 * without using sprintf. return a pointer to the last char written (\0) or
2187 * NULL if there isn't enough space.
2188 */
2189char *date2str_log(char *dst, struct tm *tm, struct timeval *date, size_t size)
2190{
2191
2192 if (size < 25) /* the size is fixed: 24 chars + \0 */
2193 return NULL;
2194
2195 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2196 *dst++ = '/';
2197 memcpy(dst, monthname[tm->tm_mon], 3); // month
2198 dst += 3;
2199 *dst++ = '/';
2200 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2201 *dst++ = ':';
2202 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2203 *dst++ = ':';
2204 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2205 *dst++ = ':';
2206 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2207 *dst++ = '.';
2208 utoa_pad((unsigned int)(date->tv_usec/1000), dst, 4); // millisecondes
2209 dst += 3; // only the 3 first digits
2210 *dst = '\0';
2211
2212 return dst;
2213}
2214
2215/* gmt2str_log: write a date in the format :
2216 * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf
2217 * return a pointer to the last char written (\0) or
2218 * NULL if there isn't enough space.
2219 */
2220char *gmt2str_log(char *dst, struct tm *tm, size_t size)
2221{
Yuxans Yao4e25b012012-10-19 10:36:09 +08002222 if (size < 27) /* the size is fixed: 26 chars + \0 */
William Lallemand421f5b52012-02-06 18:15:57 +01002223 return NULL;
2224
2225 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2226 *dst++ = '/';
2227 memcpy(dst, monthname[tm->tm_mon], 3); // month
2228 dst += 3;
2229 *dst++ = '/';
2230 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2231 *dst++ = ':';
2232 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2233 *dst++ = ':';
2234 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2235 *dst++ = ':';
2236 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2237 *dst++ = ' ';
2238 *dst++ = '+';
2239 *dst++ = '0';
2240 *dst++ = '0';
2241 *dst++ = '0';
2242 *dst++ = '0';
2243 *dst = '\0';
2244
2245 return dst;
2246}
2247
Yuxans Yao4e25b012012-10-19 10:36:09 +08002248/* localdate2str_log: write a date in the format :
2249 * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf
2250 * * return a pointer to the last char written (\0) or
2251 * * NULL if there isn't enough space.
2252 */
2253char *localdate2str_log(char *dst, struct tm *tm, size_t size)
2254{
2255 if (size < 27) /* the size is fixed: 26 chars + \0 */
2256 return NULL;
2257
2258 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2259 *dst++ = '/';
2260 memcpy(dst, monthname[tm->tm_mon], 3); // month
2261 dst += 3;
2262 *dst++ = '/';
2263 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2264 *dst++ = ':';
2265 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2266 *dst++ = ':';
2267 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2268 *dst++ = ':';
2269 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2270 *dst++ = ' ';
2271 memcpy(dst, localtimezone, 5); // timezone
2272 dst += 5;
2273 *dst = '\0';
2274
2275 return dst;
2276}
2277
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002278/* Dynamically allocates a string of the proper length to hold the formatted
2279 * output. NULL is returned on error. The caller is responsible for freeing the
2280 * memory area using free(). The resulting string is returned in <out> if the
2281 * pointer is not NULL. A previous version of <out> might be used to build the
2282 * new string, and it will be freed before returning if it is not NULL, which
2283 * makes it possible to build complex strings from iterative calls without
2284 * having to care about freeing intermediate values, as in the example below :
2285 *
2286 * memprintf(&err, "invalid argument: '%s'", arg);
2287 * ...
2288 * memprintf(&err, "parser said : <%s>\n", *err);
2289 * ...
2290 * free(*err);
2291 *
2292 * This means that <err> must be initialized to NULL before first invocation.
2293 * The return value also holds the allocated string, which eases error checking
2294 * and immediate consumption. If the output pointer is not used, NULL must be
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002295 * passed instead and it will be ignored. The returned message will then also
2296 * be NULL so that the caller does not have to bother with freeing anything.
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002297 *
2298 * It is also convenient to use it without any free except the last one :
2299 * err = NULL;
2300 * if (!fct1(err)) report(*err);
2301 * if (!fct2(err)) report(*err);
2302 * if (!fct3(err)) report(*err);
2303 * free(*err);
2304 */
2305char *memprintf(char **out, const char *format, ...)
2306{
2307 va_list args;
2308 char *ret = NULL;
2309 int allocated = 0;
2310 int needed = 0;
2311
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002312 if (!out)
2313 return NULL;
2314
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002315 do {
2316 /* vsnprintf() will return the required length even when the
2317 * target buffer is NULL. We do this in a loop just in case
2318 * intermediate evaluations get wrong.
2319 */
2320 va_start(args, format);
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002321 needed = vsnprintf(ret, allocated, format, args);
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002322 va_end(args);
2323
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002324 if (needed < allocated) {
2325 /* Note: on Solaris 8, the first iteration always
2326 * returns -1 if allocated is zero, so we force a
2327 * retry.
2328 */
2329 if (!allocated)
2330 needed = 0;
2331 else
2332 break;
2333 }
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002334
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002335 allocated = needed + 1;
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002336 ret = realloc(ret, allocated);
2337 } while (ret);
2338
2339 if (needed < 0) {
2340 /* an error was encountered */
2341 free(ret);
2342 ret = NULL;
2343 }
2344
2345 if (out) {
2346 free(*out);
2347 *out = ret;
2348 }
2349
2350 return ret;
2351}
William Lallemand421f5b52012-02-06 18:15:57 +01002352
Willy Tarreau21c705b2012-09-14 11:40:36 +02002353/* Used to add <level> spaces before each line of <out>, unless there is only one line.
2354 * The input argument is automatically freed and reassigned. The result will have to be
Willy Tarreau70eec382012-10-10 08:56:47 +02002355 * freed by the caller. It also supports being passed a NULL which results in the same
2356 * output.
Willy Tarreau21c705b2012-09-14 11:40:36 +02002357 * Example of use :
2358 * parse(cmd, &err); (callee: memprintf(&err, ...))
2359 * fprintf(stderr, "Parser said: %s\n", indent_error(&err));
2360 * free(err);
2361 */
2362char *indent_msg(char **out, int level)
2363{
2364 char *ret, *in, *p;
2365 int needed = 0;
2366 int lf = 0;
2367 int lastlf = 0;
2368 int len;
2369
Willy Tarreau70eec382012-10-10 08:56:47 +02002370 if (!out || !*out)
2371 return NULL;
2372
Willy Tarreau21c705b2012-09-14 11:40:36 +02002373 in = *out - 1;
2374 while ((in = strchr(in + 1, '\n')) != NULL) {
2375 lastlf = in - *out;
2376 lf++;
2377 }
2378
2379 if (!lf) /* single line, no LF, return it as-is */
2380 return *out;
2381
2382 len = strlen(*out);
2383
2384 if (lf == 1 && lastlf == len - 1) {
2385 /* single line, LF at end, strip it and return as-is */
2386 (*out)[lastlf] = 0;
2387 return *out;
2388 }
2389
2390 /* OK now we have at least one LF, we need to process the whole string
2391 * as a multi-line string. What we'll do :
2392 * - prefix with an LF if there is none
2393 * - add <level> spaces before each line
2394 * This means at most ( 1 + level + (len-lf) + lf*<1+level) ) =
2395 * 1 + level + len + lf * level = 1 + level * (lf + 1) + len.
2396 */
2397
2398 needed = 1 + level * (lf + 1) + len + 1;
2399 p = ret = malloc(needed);
2400 in = *out;
2401
2402 /* skip initial LFs */
2403 while (*in == '\n')
2404 in++;
2405
2406 /* copy each line, prefixed with LF and <level> spaces, and without the trailing LF */
2407 while (*in) {
2408 *p++ = '\n';
2409 memset(p, ' ', level);
2410 p += level;
2411 do {
2412 *p++ = *in++;
2413 } while (*in && *in != '\n');
2414 if (*in)
2415 in++;
2416 }
2417 *p = 0;
2418
2419 free(*out);
2420 *out = ret;
2421
2422 return ret;
2423}
2424
Willy Tarreaudad36a32013-03-11 01:20:04 +01002425/* Convert occurrences of environment variables in the input string to their
2426 * corresponding value. A variable is identified as a series of alphanumeric
2427 * characters or underscores following a '$' sign. The <in> string must be
2428 * free()able. NULL returns NULL. The resulting string might be reallocated if
2429 * some expansion is made. Variable names may also be enclosed into braces if
2430 * needed (eg: to concatenate alphanum characters).
2431 */
2432char *env_expand(char *in)
2433{
2434 char *txt_beg;
2435 char *out;
2436 char *txt_end;
2437 char *var_beg;
2438 char *var_end;
2439 char *value;
2440 char *next;
2441 int out_len;
2442 int val_len;
2443
2444 if (!in)
2445 return in;
2446
2447 value = out = NULL;
2448 out_len = 0;
2449
2450 txt_beg = in;
2451 do {
2452 /* look for next '$' sign in <in> */
2453 for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++);
2454
2455 if (!*txt_end && !out) /* end and no expansion performed */
2456 return in;
2457
2458 val_len = 0;
2459 next = txt_end;
2460 if (*txt_end == '$') {
2461 char save;
2462
2463 var_beg = txt_end + 1;
2464 if (*var_beg == '{')
2465 var_beg++;
2466
2467 var_end = var_beg;
2468 while (isalnum((int)(unsigned char)*var_end) || *var_end == '_') {
2469 var_end++;
2470 }
2471
2472 next = var_end;
2473 if (*var_end == '}' && (var_beg > txt_end + 1))
2474 next++;
2475
2476 /* get value of the variable name at this location */
2477 save = *var_end;
2478 *var_end = '\0';
2479 value = getenv(var_beg);
2480 *var_end = save;
2481 val_len = value ? strlen(value) : 0;
2482 }
2483
2484 out = realloc(out, out_len + (txt_end - txt_beg) + val_len + 1);
2485 if (txt_end > txt_beg) {
2486 memcpy(out + out_len, txt_beg, txt_end - txt_beg);
2487 out_len += txt_end - txt_beg;
2488 }
2489 if (val_len) {
2490 memcpy(out + out_len, value, val_len);
2491 out_len += val_len;
2492 }
2493 out[out_len] = 0;
2494 txt_beg = next;
2495 } while (*txt_beg);
2496
2497 /* here we know that <out> was allocated and that we don't need <in> anymore */
2498 free(in);
2499 return out;
2500}
2501
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002502
2503/* same as strstr() but case-insensitive and with limit length */
2504const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2)
2505{
2506 char *pptr, *sptr, *start;
Willy Tarreauc8746532014-05-28 23:05:07 +02002507 unsigned int slen, plen;
2508 unsigned int tmp1, tmp2;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002509
2510 if (str1 == NULL || len_str1 == 0) // search pattern into an empty string => search is not found
2511 return NULL;
2512
2513 if (str2 == NULL || len_str2 == 0) // pattern is empty => every str1 match
2514 return str1;
2515
2516 if (len_str1 < len_str2) // pattern is longer than string => search is not found
2517 return NULL;
2518
2519 for (tmp1 = 0, start = (char *)str1, pptr = (char *)str2, slen = len_str1, plen = len_str2; slen >= plen; start++, slen--) {
2520 while (toupper(*start) != toupper(*str2)) {
2521 start++;
2522 slen--;
2523 tmp1++;
2524
2525 if (tmp1 >= len_str1)
2526 return NULL;
2527
2528 /* if pattern longer than string */
2529 if (slen < plen)
2530 return NULL;
2531 }
2532
2533 sptr = start;
2534 pptr = (char *)str2;
2535
2536 tmp2 = 0;
2537 while (toupper(*sptr) == toupper(*pptr)) {
2538 sptr++;
2539 pptr++;
2540 tmp2++;
2541
2542 if (*pptr == '\0' || tmp2 == len_str2) /* end of pattern found */
2543 return start;
2544 if (*sptr == '\0' || tmp2 == len_str1) /* end of string found and the pattern is not fully found */
2545 return NULL;
2546 }
2547 }
2548 return NULL;
2549}
2550
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002551/* This function read the next valid utf8 char.
2552 * <s> is the byte srray to be decode, <len> is its length.
2553 * The function returns decoded char encoded like this:
2554 * The 4 msb are the return code (UTF8_CODE_*), the 4 lsb
2555 * are the length read. The decoded character is stored in <c>.
2556 */
2557unsigned char utf8_next(const char *s, int len, unsigned int *c)
2558{
2559 const unsigned char *p = (unsigned char *)s;
2560 int dec;
2561 unsigned char code = UTF8_CODE_OK;
2562
2563 if (len < 1)
2564 return UTF8_CODE_OK;
2565
2566 /* Check the type of UTF8 sequence
2567 *
2568 * 0... .... 0x00 <= x <= 0x7f : 1 byte: ascii char
2569 * 10.. .... 0x80 <= x <= 0xbf : invalid sequence
2570 * 110. .... 0xc0 <= x <= 0xdf : 2 bytes
2571 * 1110 .... 0xe0 <= x <= 0xef : 3 bytes
2572 * 1111 0... 0xf0 <= x <= 0xf7 : 4 bytes
2573 * 1111 10.. 0xf8 <= x <= 0xfb : 5 bytes
2574 * 1111 110. 0xfc <= x <= 0xfd : 6 bytes
2575 * 1111 111. 0xfe <= x <= 0xff : invalid sequence
2576 */
2577 switch (*p) {
2578 case 0x00 ... 0x7f:
2579 *c = *p;
2580 return UTF8_CODE_OK | 1;
2581
2582 case 0x80 ... 0xbf:
2583 *c = *p;
2584 return UTF8_CODE_BADSEQ | 1;
2585
2586 case 0xc0 ... 0xdf:
2587 if (len < 2) {
2588 *c = *p;
2589 return UTF8_CODE_BADSEQ | 1;
2590 }
2591 *c = *p & 0x1f;
2592 dec = 1;
2593 break;
2594
2595 case 0xe0 ... 0xef:
2596 if (len < 3) {
2597 *c = *p;
2598 return UTF8_CODE_BADSEQ | 1;
2599 }
2600 *c = *p & 0x0f;
2601 dec = 2;
2602 break;
2603
2604 case 0xf0 ... 0xf7:
2605 if (len < 4) {
2606 *c = *p;
2607 return UTF8_CODE_BADSEQ | 1;
2608 }
2609 *c = *p & 0x07;
2610 dec = 3;
2611 break;
2612
2613 case 0xf8 ... 0xfb:
2614 if (len < 5) {
2615 *c = *p;
2616 return UTF8_CODE_BADSEQ | 1;
2617 }
2618 *c = *p & 0x03;
2619 dec = 4;
2620 break;
2621
2622 case 0xfc ... 0xfd:
2623 if (len < 6) {
2624 *c = *p;
2625 return UTF8_CODE_BADSEQ | 1;
2626 }
2627 *c = *p & 0x01;
2628 dec = 5;
2629 break;
2630
2631 case 0xfe ... 0xff:
2632 default:
2633 *c = *p;
2634 return UTF8_CODE_BADSEQ | 1;
2635 }
2636
2637 p++;
2638
2639 while (dec > 0) {
2640
2641 /* need 0x10 for the 2 first bits */
2642 if ( ( *p & 0xc0 ) != 0x80 )
2643 return UTF8_CODE_BADSEQ | ((p-(unsigned char *)s)&0xffff);
2644
2645 /* add data at char */
2646 *c = ( *c << 6 ) | ( *p & 0x3f );
2647
2648 dec--;
2649 p++;
2650 }
2651
2652 /* Check ovelong encoding.
2653 * 1 byte : 5 + 6 : 11 : 0x80 ... 0x7ff
2654 * 2 bytes : 4 + 6 + 6 : 16 : 0x800 ... 0xffff
2655 * 3 bytes : 3 + 6 + 6 + 6 : 21 : 0x10000 ... 0x1fffff
2656 */
2657 if ((*c >= 0x00 && *c <= 0x7f && (p-(unsigned char *)s) > 1) ||
2658 (*c >= 0x80 && *c <= 0x7ff && (p-(unsigned char *)s) > 2) ||
2659 (*c >= 0x800 && *c <= 0xffff && (p-(unsigned char *)s) > 3) ||
2660 (*c >= 0x10000 && *c <= 0x1fffff && (p-(unsigned char *)s) > 4))
2661 code |= UTF8_CODE_OVERLONG;
2662
2663 /* Check invalid UTF8 range. */
2664 if ((*c >= 0xd800 && *c <= 0xdfff) ||
2665 (*c >= 0xfffe && *c <= 0xffff))
2666 code |= UTF8_CODE_INVRANGE;
2667
2668 return code | ((p-(unsigned char *)s)&0x0f);
2669}
2670
Willy Tarreaubaaee002006-06-26 02:48:02 +02002671/*
2672 * Local variables:
2673 * c-indent-level: 8
2674 * c-basic-offset: 8
2675 * End:
2676 */