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