blob: 00e672add4fee380a6f9f9a6f88f47f0ad88f7b8 [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 Tarreau2e74c3f2007-12-02 18:45:09 +0100507/*
508 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
509 * invalid character is found, a pointer to it is returned. If everything is
510 * fine, NULL is returned.
511 */
512const char *invalid_char(const char *name)
513{
514 if (!*name)
515 return name;
516
517 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100518 if (!isalnum((int)(unsigned char)*name) && *name != '.' && *name != ':' &&
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100519 *name != '_' && *name != '-')
520 return name;
521 name++;
522 }
523 return NULL;
524}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200525
526/*
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200527 * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-].
528 * If an invalid character is found, a pointer to it is returned.
529 * If everything is fine, NULL is returned.
530 */
531const char *invalid_domainchar(const char *name) {
532
533 if (!*name)
534 return name;
535
536 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100537 if (!isalnum((int)(unsigned char)*name) && *name != '.' &&
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200538 *name != '_' && *name != '-')
539 return name;
540
541 name++;
542 }
543
544 return NULL;
545}
546
547/*
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100548 * converts <str> to a struct sockaddr_storage* provided by the caller. The
Willy Tarreau24709282013-03-10 21:32:12 +0100549 * caller must have zeroed <sa> first, and may have set sa->ss_family to force
550 * parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then
551 * the function tries to guess the address family from the syntax. If the
552 * family is forced and the format doesn't match, an error is returned. The
Willy Tarreaufab5a432011-03-04 15:31:53 +0100553 * string is assumed to contain only an address, no port. The address can be a
554 * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to
555 * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved.
556 * The return address will only have the address family and the address set,
557 * all other fields remain zero. The string is not supposed to be modified.
558 * The IPv6 '::' address is IN6ADDR_ANY.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200559 */
Willy Tarreau24709282013-03-10 21:32:12 +0100560static struct sockaddr_storage *str2ip(const char *str, struct sockaddr_storage *sa)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200561{
Willy Tarreaufab5a432011-03-04 15:31:53 +0100562 struct hostent *he;
563
Willy Tarreaufab5a432011-03-04 15:31:53 +0100564 /* Any IPv6 address */
565 if (str[0] == ':' && str[1] == ':' && !str[2]) {
Willy Tarreau24709282013-03-10 21:32:12 +0100566 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
567 sa->ss_family = AF_INET6;
568 else if (sa->ss_family != AF_INET6)
569 goto fail;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100570 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100571 }
572
Willy Tarreau24709282013-03-10 21:32:12 +0100573 /* Any address for the family, defaults to IPv4 */
Willy Tarreaufab5a432011-03-04 15:31:53 +0100574 if (!str[0] || (str[0] == '*' && !str[1])) {
Willy Tarreau24709282013-03-10 21:32:12 +0100575 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
576 sa->ss_family = AF_INET;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100577 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100578 }
579
580 /* check for IPv6 first */
Willy Tarreau24709282013-03-10 21:32:12 +0100581 if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET6) &&
582 inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)sa)->sin6_addr)) {
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100583 sa->ss_family = AF_INET6;
584 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100585 }
586
587 /* then check for IPv4 */
Willy Tarreau24709282013-03-10 21:32:12 +0100588 if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET) &&
589 inet_pton(AF_INET, str, &((struct sockaddr_in *)sa)->sin_addr)) {
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100590 sa->ss_family = AF_INET;
591 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100592 }
593
David du Colombierd5f43282011-03-17 10:40:16 +0100594#ifdef USE_GETADDRINFO
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200595 if (global.tune.options & GTUNE_USE_GAI) {
David du Colombierd5f43282011-03-17 10:40:16 +0100596 struct addrinfo hints, *result;
597
598 memset(&result, 0, sizeof(result));
599 memset(&hints, 0, sizeof(hints));
Willy Tarreau24709282013-03-10 21:32:12 +0100600 hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC;
David du Colombierd5f43282011-03-17 10:40:16 +0100601 hints.ai_socktype = SOCK_DGRAM;
602 hints.ai_flags = AI_PASSIVE;
603 hints.ai_protocol = 0;
604
605 if (getaddrinfo(str, NULL, &hints, &result) == 0) {
Willy Tarreau24709282013-03-10 21:32:12 +0100606 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
607 sa->ss_family = result->ai_family;
608 else if (sa->ss_family != result->ai_family)
609 goto fail;
610
David du Colombierd5f43282011-03-17 10:40:16 +0100611 switch (result->ai_family) {
612 case AF_INET:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100613 memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen);
614 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100615 case AF_INET6:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100616 memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen);
617 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100618 }
619 }
620
Sean Carey58ea0392013-02-15 23:39:18 +0100621 if (result)
622 freeaddrinfo(result);
Willy Tarreaufab5a432011-03-04 15:31:53 +0100623 }
David du Colombierd5f43282011-03-17 10:40:16 +0100624#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200625 /* try to resolve an IPv4/IPv6 hostname */
626 he = gethostbyname(str);
627 if (he) {
628 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
629 sa->ss_family = he->h_addrtype;
630 else if (sa->ss_family != he->h_addrtype)
631 goto fail;
632
633 switch (sa->ss_family) {
634 case AF_INET:
635 ((struct sockaddr_in *)sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
636 return sa;
637 case AF_INET6:
638 ((struct sockaddr_in6 *)sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
639 return sa;
640 }
641 }
642
David du Colombierd5f43282011-03-17 10:40:16 +0100643 /* unsupported address family */
Willy Tarreau24709282013-03-10 21:32:12 +0100644 fail:
Willy Tarreaufab5a432011-03-04 15:31:53 +0100645 return NULL;
646}
647
648/*
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100649 * Converts <str> to a locally allocated struct sockaddr_storage *, and a port
650 * range or offset consisting in two integers that the caller will have to
651 * check to find the relevant input format. The following format are supported :
652 *
653 * String format | address | port | low | high
654 * addr | <addr> | 0 | 0 | 0
655 * addr: | <addr> | 0 | 0 | 0
656 * addr:port | <addr> | <port> | <port> | <port>
657 * addr:pl-ph | <addr> | <pl> | <pl> | <ph>
658 * addr:+port | <addr> | <port> | 0 | <port>
659 * addr:-port | <addr> |-<port> | <port> | 0
660 *
661 * The detection of a port range or increment by the caller is made by
662 * comparing <low> and <high>. If both are equal, then port 0 means no port
663 * was specified. The caller may pass NULL for <low> and <high> if it is not
664 * interested in retrieving port ranges.
665 *
666 * Note that <addr> above may also be :
667 * - empty ("") => family will be AF_INET and address will be INADDR_ANY
668 * - "*" => family will be AF_INET and address will be INADDR_ANY
669 * - "::" => family will be AF_INET6 and address will be IN6ADDR_ANY
670 * - a host name => family and address will depend on host name resolving.
671 *
Willy Tarreau24709282013-03-10 21:32:12 +0100672 * A prefix may be passed in before the address above to force the family :
673 * - "ipv4@" => force address to resolve as IPv4 and fail if not possible.
674 * - "ipv6@" => force address to resolve as IPv6 and fail if not possible.
675 * - "unix@" => force address to be a path to a UNIX socket even if the
676 * path does not start with a '/'
Willy Tarreauccfccef2014-05-10 01:49:15 +0200677 * - 'abns@' -> force address to belong to the abstract namespace (Linux
678 * only). These sockets are just like Unix sockets but without
679 * the need for an underlying file system. The address is a
680 * string. Technically it's like a Unix socket with a zero in
681 * the first byte of the address.
Willy Tarreau40aa0702013-03-10 23:51:38 +0100682 * - "fd@" => an integer must follow, and is a file descriptor number.
Willy Tarreau24709282013-03-10 21:32:12 +0100683 *
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100684 * Also note that in order to avoid any ambiguity with IPv6 addresses, the ':'
685 * is mandatory after the IP address even when no port is specified. NULL is
686 * returned if the address cannot be parsed. The <low> and <high> ports are
Willy Tarreau24709282013-03-10 21:32:12 +0100687 * always initialized if non-null, even for non-IP families.
Willy Tarreaud393a622013-03-04 18:22:00 +0100688 *
689 * If <pfx> is non-null, it is used as a string prefix before any path-based
690 * address (typically the path to a unix socket).
Willy Tarreau40aa0702013-03-10 23:51:38 +0100691 *
692 * When a file descriptor is passed, its value is put into the s_addr part of
693 * the address when cast to sockaddr_in and the address family is AF_UNSPEC.
Willy Tarreaufab5a432011-03-04 15:31:53 +0100694 */
Willy Tarreaud393a622013-03-04 18:22:00 +0100695struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high, char **err, const char *pfx)
Willy Tarreaufab5a432011-03-04 15:31:53 +0100696{
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100697 static struct sockaddr_storage ss;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100698 struct sockaddr_storage *ret = NULL;
Willy Tarreau24709282013-03-10 21:32:12 +0100699 char *back, *str2;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100700 char *port1, *port2;
701 int portl, porth, porta;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200702 int abstract = 0;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100703
704 portl = porth = porta = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200705
Willy Tarreaudad36a32013-03-11 01:20:04 +0100706 str2 = back = env_expand(strdup(str));
Willy Tarreaudf350f12013-03-01 20:22:54 +0100707 if (str2 == NULL) {
708 memprintf(err, "out of memory in '%s'\n", __FUNCTION__);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100709 goto out;
Willy Tarreaudf350f12013-03-01 20:22:54 +0100710 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200711
Willy Tarreau24709282013-03-10 21:32:12 +0100712 memset(&ss, 0, sizeof(ss));
713
714 if (strncmp(str2, "unix@", 5) == 0) {
715 str2 += 5;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200716 abstract = 0;
Willy Tarreau24709282013-03-10 21:32:12 +0100717 ss.ss_family = AF_UNIX;
718 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200719 else if (strncmp(str2, "abns@", 5) == 0) {
720 str2 += 5;
721 abstract = 1;
722 ss.ss_family = AF_UNIX;
723 }
Willy Tarreau24709282013-03-10 21:32:12 +0100724 else if (strncmp(str2, "ipv4@", 5) == 0) {
725 str2 += 5;
726 ss.ss_family = AF_INET;
727 }
728 else if (strncmp(str2, "ipv6@", 5) == 0) {
729 str2 += 5;
730 ss.ss_family = AF_INET6;
731 }
732 else if (*str2 == '/') {
733 ss.ss_family = AF_UNIX;
734 }
735 else
736 ss.ss_family = AF_UNSPEC;
737
Willy Tarreau40aa0702013-03-10 23:51:38 +0100738 if (ss.ss_family == AF_UNSPEC && strncmp(str2, "fd@", 3) == 0) {
739 char *endptr;
740
741 str2 += 3;
742 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = strtol(str2, &endptr, 10);
743
744 if (!*str2 || *endptr) {
Willy Tarreaudad36a32013-03-11 01:20:04 +0100745 memprintf(err, "file descriptor '%s' is not a valid integer in '%s'\n", str2, str);
Willy Tarreau40aa0702013-03-10 23:51:38 +0100746 goto out;
747 }
748
749 /* we return AF_UNSPEC if we use a file descriptor number */
750 ss.ss_family = AF_UNSPEC;
751 }
752 else if (ss.ss_family == AF_UNIX) {
Willy Tarreau15586382013-03-04 19:48:14 +0100753 int prefix_path_len;
754 int max_path_len;
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200755 int adr_len;
Willy Tarreau15586382013-03-04 19:48:14 +0100756
757 /* complete unix socket path name during startup or soft-restart is
758 * <unix_bind_prefix><path>.<pid>.<bak|tmp>
759 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200760 prefix_path_len = (pfx && !abstract) ? strlen(pfx) : 0;
Willy Tarreau15586382013-03-04 19:48:14 +0100761 max_path_len = (sizeof(((struct sockaddr_un *)&ss)->sun_path) - 1) -
762 (prefix_path_len ? prefix_path_len + 1 + 5 + 1 + 3 : 0);
763
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200764 adr_len = strlen(str2);
765 if (adr_len > max_path_len) {
Willy Tarreau15586382013-03-04 19:48:14 +0100766 memprintf(err, "socket path '%s' too long (max %d)\n", str, max_path_len);
767 goto out;
768 }
769
Willy Tarreauccfccef2014-05-10 01:49:15 +0200770 /* when abstract==1, we skip the first zero and copy all bytes except the trailing zero */
771 memset(((struct sockaddr_un *)&ss)->sun_path, 0, sizeof(((struct sockaddr_un *)&ss)->sun_path));
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200772 if (prefix_path_len)
Willy Tarreau15586382013-03-04 19:48:14 +0100773 memcpy(((struct sockaddr_un *)&ss)->sun_path, pfx, prefix_path_len);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200774 memcpy(((struct sockaddr_un *)&ss)->sun_path + prefix_path_len + abstract, str2, adr_len + 1 - abstract);
Willy Tarreau15586382013-03-04 19:48:14 +0100775 }
Willy Tarreau24709282013-03-10 21:32:12 +0100776 else { /* IPv4 and IPv6 */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100777 port1 = strrchr(str2, ':');
778 if (port1)
779 *port1++ = '\0';
780 else
781 port1 = "";
Willy Tarreaubaaee002006-06-26 02:48:02 +0200782
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100783 if (str2ip(str2, &ss) == NULL) {
784 memprintf(err, "invalid address: '%s' in '%s'\n", str2, str);
785 goto out;
786 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100787
Willy Tarreaua39d1992013-04-01 20:37:42 +0200788 if (isdigit((int)(unsigned char)*port1)) { /* single port or range */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100789 port2 = strchr(port1, '-');
790 if (port2)
791 *port2++ = '\0';
792 else
793 port2 = port1;
794 portl = atoi(port1);
795 porth = atoi(port2);
796 porta = portl;
797 }
798 else if (*port1 == '-') { /* negative offset */
799 portl = atoi(port1 + 1);
800 porta = -portl;
801 }
802 else if (*port1 == '+') { /* positive offset */
803 porth = atoi(port1 + 1);
804 porta = porth;
805 }
806 else if (*port1) { /* other any unexpected char */
Willy Tarreaudad36a32013-03-11 01:20:04 +0100807 memprintf(err, "invalid character '%c' in port number '%s' in '%s'\n", *port1, port1, str);
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100808 goto out;
809 }
810 set_host_port(&ss, porta);
Willy Tarreaue4c58c82013-03-06 15:28:17 +0100811 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100812
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100813 ret = &ss;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100814 out:
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100815 if (low)
816 *low = portl;
817 if (high)
818 *high = porth;
Willy Tarreau24709282013-03-10 21:32:12 +0100819 free(back);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100820 return ret;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200821}
822
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100823/* converts <str> to a struct in_addr containing a network mask. It can be
824 * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1
825 * if the conversion succeeds otherwise non-zero.
826 */
827int str2mask(const char *str, struct in_addr *mask)
828{
829 if (strchr(str, '.') != NULL) { /* dotted notation */
830 if (!inet_pton(AF_INET, str, mask))
831 return 0;
832 }
833 else { /* mask length */
834 char *err;
835 unsigned long len = strtol(str, &err, 10);
836
837 if (!*str || (err && *err) || (unsigned)len > 32)
838 return 0;
839 if (len)
840 mask->s_addr = htonl(~0UL << (32 - len));
841 else
842 mask->s_addr = 0;
843 }
844 return 1;
845}
846
Thierry FOURNIERb0504632013-12-14 15:39:02 +0100847/* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
848 * succeeds otherwise zero.
849 */
850int cidr2dotted(int cidr, struct in_addr *mask) {
851
852 if (cidr < 0 || cidr > 32)
853 return 0;
854
855 mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0;
856 return 1;
857}
858
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200859/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200860 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200861 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
862 * is optionnal and either in the dotted or CIDR notation.
863 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
864 */
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100865int str2net(const char *str, int resolve, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200866{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200867 __label__ out_free, out_err;
868 char *c, *s;
869 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200870
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200871 s = strdup(str);
872 if (!s)
873 return 0;
874
Willy Tarreaubaaee002006-06-26 02:48:02 +0200875 memset(mask, 0, sizeof(*mask));
876 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200877
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200878 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200879 *c++ = '\0';
880 /* c points to the mask */
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100881 if (!str2mask(c, mask))
882 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200883 }
884 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100885 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200886 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200887 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200888 struct hostent *he;
889
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100890 if (!resolve)
891 goto out_err;
892
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200893 if ((he = gethostbyname(s)) == NULL) {
894 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200895 }
896 else
897 *addr = *(struct in_addr *) *(he->h_addr_list);
898 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200899
900 ret_val = 1;
901 out_free:
902 free(s);
903 return ret_val;
904 out_err:
905 ret_val = 0;
906 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200907}
908
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100909
910/*
Willy Tarreau6d20e282012-04-27 22:49:47 +0200911 * converts <str> to two struct in6_addr* which must be pre-allocated.
912 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
913 * is an optionnal number of bits (128 being the default).
914 * Returns 1 if OK, 0 if error.
915 */
916int str62net(const char *str, struct in6_addr *addr, unsigned char *mask)
917{
918 char *c, *s;
919 int ret_val = 0;
920 char *err;
921 unsigned long len = 128;
922
923 s = strdup(str);
924 if (!s)
925 return 0;
926
927 memset(mask, 0, sizeof(*mask));
928 memset(addr, 0, sizeof(*addr));
929
930 if ((c = strrchr(s, '/')) != NULL) {
931 *c++ = '\0'; /* c points to the mask */
932 if (!*c)
933 goto out_free;
934
935 len = strtoul(c, &err, 10);
936 if ((err && *err) || (unsigned)len > 128)
937 goto out_free;
938 }
939 *mask = len; /* OK we have a valid mask in <len> */
940
941 if (!inet_pton(AF_INET6, s, addr))
942 goto out_free;
943
944 ret_val = 1;
945 out_free:
946 free(s);
947 return ret_val;
948}
949
950
951/*
David du Colombier6f5ccb12011-03-10 22:26:24 +0100952 * Parse IPv4 address found in url.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100953 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100954int url2ipv4(const char *addr, struct in_addr *dst)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100955{
956 int saw_digit, octets, ch;
957 u_char tmp[4], *tp;
958 const char *cp = addr;
959
960 saw_digit = 0;
961 octets = 0;
962 *(tp = tmp) = 0;
963
964 while (*addr) {
965 unsigned char digit = (ch = *addr++) - '0';
966 if (digit > 9 && ch != '.')
967 break;
968 if (digit <= 9) {
969 u_int new = *tp * 10 + digit;
970 if (new > 255)
971 return 0;
972 *tp = new;
973 if (!saw_digit) {
974 if (++octets > 4)
975 return 0;
976 saw_digit = 1;
977 }
978 } else if (ch == '.' && saw_digit) {
979 if (octets == 4)
980 return 0;
981 *++tp = 0;
982 saw_digit = 0;
983 } else
984 return 0;
985 }
986
987 if (octets < 4)
988 return 0;
989
990 memcpy(&dst->s_addr, tmp, 4);
991 return addr-cp-1;
992}
993
994/*
Thierry FOURNIER9f95e402014-03-21 14:51:46 +0100995 * Resolve destination server from URL. Convert <str> to a sockaddr_storage.
996 * <out> contain the code of the dectected scheme, the start and length of
997 * the hostname. Actually only http and https are supported. <out> can be NULL.
998 * This function returns the consumed length. It is useful if you parse complete
999 * url like http://host:port/path, because the consumed length corresponds to
1000 * the first character of the path. If the conversion fails, it returns -1.
1001 *
1002 * This function tries to resolve the DNS name if haproxy is in starting mode.
1003 * So, this function may be used during the configuration parsing.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001004 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001005int url2sa(const char *url, int ulen, struct sockaddr_storage *addr, struct split_url *out)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001006{
1007 const char *curr = url, *cp = url;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001008 const char *end;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001009 int ret, url_code = 0;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001010 unsigned long long int http_code = 0;
1011 int default_port;
1012 struct hostent *he;
1013 char *p;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001014
1015 /* Firstly, try to find :// pattern */
1016 while (curr < url+ulen && url_code != 0x3a2f2f) {
1017 url_code = ((url_code & 0xffff) << 8);
1018 url_code += (unsigned char)*curr++;
1019 }
1020
1021 /* Secondly, if :// pattern is found, verify parsed stuff
1022 * before pattern is matching our http pattern.
1023 * If so parse ip address and port in uri.
1024 *
1025 * WARNING: Current code doesn't support dynamic async dns resolver.
1026 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001027 if (url_code != 0x3a2f2f)
1028 return -1;
1029
1030 /* Copy scheme, and utrn to lower case. */
1031 while (cp < curr - 3)
1032 http_code = (http_code << 8) + *cp++;
1033 http_code |= 0x2020202020202020ULL; /* Turn everything to lower case */
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001034
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001035 /* HTTP or HTTPS url matching */
1036 if (http_code == 0x2020202068747470ULL) {
1037 default_port = 80;
1038 if (out)
1039 out->scheme = SCH_HTTP;
1040 }
1041 else if (http_code == 0x2020206874747073ULL) {
1042 default_port = 443;
1043 if (out)
1044 out->scheme = SCH_HTTPS;
1045 }
1046 else
1047 return -1;
1048
1049 /* If the next char is '[', the host address is IPv6. */
1050 if (*curr == '[') {
1051 curr++;
1052
1053 /* Check trash size */
1054 if (trash.size < ulen)
1055 return -1;
1056
1057 /* Look for ']' and copy the address in a trash buffer. */
1058 p = trash.str;
1059 for (end = curr;
1060 end < url + ulen && *end != ']';
1061 end++, p++)
1062 *p = *end;
1063 if (*end != ']')
1064 return -1;
1065 *p = '\0';
1066
1067 /* Update out. */
1068 if (out) {
1069 out->host = curr;
1070 out->host_len = end - curr;
1071 }
1072
1073 /* Try IPv6 decoding. */
1074 if (!inet_pton(AF_INET6, trash.str, &((struct sockaddr_in6 *)addr)->sin6_addr))
1075 return -1;
1076 end++;
1077
1078 /* Decode port. */
1079 if (*end == ':') {
1080 end++;
1081 default_port = read_uint(&end, url + ulen);
1082 }
1083 ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port);
1084 ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
1085 return end - url;
1086 }
1087 else {
1088 /* We are looking for IP address. If you want to parse and
1089 * resolve hostname found in url, you can use str2sa_range(), but
1090 * be warned this can slow down global daemon performances
1091 * while handling lagging dns responses.
1092 */
1093 ret = url2ipv4(curr, &((struct sockaddr_in *)addr)->sin_addr);
1094 if (ret) {
1095 /* Update out. */
1096 if (out) {
1097 out->host = curr;
1098 out->host_len = ret;
1099 }
1100
1101 curr += ret;
1102
1103 /* Decode port. */
1104 if (*curr == ':') {
1105 curr++;
1106 default_port = read_uint(&curr, url + ulen);
1107 }
1108 ((struct sockaddr_in *)addr)->sin_port = htons(default_port);
1109
1110 /* Set family. */
1111 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
1112 return curr - url;
1113 }
1114 else if (global.mode & MODE_STARTING) {
1115 /* The IPv4 and IPv6 decoding fails, maybe the url contain name. Try to execute
1116 * synchronous DNS request only if HAProxy is in the start state.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001117 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001118
1119 /* look for : or / or end */
1120 for (end = curr;
1121 end < url + ulen && *end != '/' && *end != ':';
1122 end++);
1123 memcpy(trash.str, curr, end - curr);
1124 trash.str[end - curr] = '\0';
1125
1126 /* try to resolve an IPv4/IPv6 hostname */
1127 he = gethostbyname(trash.str);
1128 if (!he)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001129 return -1;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001130
1131 /* Update out. */
1132 if (out) {
1133 out->host = curr;
1134 out->host_len = end - curr;
1135 }
1136
1137 /* Decode port. */
1138 if (*end == ':') {
1139 end++;
1140 default_port = read_uint(&end, url + ulen);
1141 }
1142
1143 /* Copy IP address, set port and family. */
1144 switch (he->h_addrtype) {
1145 case AF_INET:
1146 ((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
1147 ((struct sockaddr_in *)addr)->sin_port = htons(default_port);
1148 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
1149 return end - url;
1150
1151 case AF_INET6:
1152 ((struct sockaddr_in6 *)addr)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
1153 ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port);
1154 ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
1155 return end - url;
1156 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001157 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001158 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001159 return -1;
1160}
1161
Willy Tarreau631f01c2011-09-05 00:36:48 +02001162/* Tries to convert a sockaddr_storage address to text form. Upon success, the
1163 * address family is returned so that it's easy for the caller to adapt to the
1164 * output format. Zero is returned if the address family is not supported. -1
1165 * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
1166 * supported.
1167 */
1168int addr_to_str(struct sockaddr_storage *addr, char *str, int size)
1169{
1170
1171 void *ptr;
1172
1173 if (size < 5)
1174 return 0;
1175 *str = '\0';
1176
1177 switch (addr->ss_family) {
1178 case AF_INET:
1179 ptr = &((struct sockaddr_in *)addr)->sin_addr;
1180 break;
1181 case AF_INET6:
1182 ptr = &((struct sockaddr_in6 *)addr)->sin6_addr;
1183 break;
1184 case AF_UNIX:
1185 memcpy(str, "unix", 5);
1186 return addr->ss_family;
1187 default:
1188 return 0;
1189 }
1190
1191 if (inet_ntop(addr->ss_family, ptr, str, size))
1192 return addr->ss_family;
1193
1194 /* failed */
1195 return -1;
1196}
1197
Simon Horman75ab8bd2014-06-16 09:39:41 +09001198/* Tries to convert a sockaddr_storage port to text form. Upon success, the
1199 * address family is returned so that it's easy for the caller to adapt to the
1200 * output format. Zero is returned if the address family is not supported. -1
1201 * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
1202 * supported.
1203 */
1204int port_to_str(struct sockaddr_storage *addr, char *str, int size)
1205{
1206
1207 uint16_t port;
1208
1209
1210 if (size < 5)
1211 return 0;
1212 *str = '\0';
1213
1214 switch (addr->ss_family) {
1215 case AF_INET:
1216 port = ((struct sockaddr_in *)addr)->sin_port;
1217 break;
1218 case AF_INET6:
1219 port = ((struct sockaddr_in6 *)addr)->sin6_port;
1220 break;
1221 case AF_UNIX:
1222 memcpy(str, "unix", 5);
1223 return addr->ss_family;
1224 default:
1225 return 0;
1226 }
1227
1228 snprintf(str, size, "%u", ntohs(port));
1229 return addr->ss_family;
1230}
1231
Willy Tarreaubaaee002006-06-26 02:48:02 +02001232/* will try to encode the string <string> replacing all characters tagged in
1233 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
1234 * prefixed by <escape>, and will store the result between <start> (included)
1235 * and <stop> (excluded), and will always terminate the string with a '\0'
1236 * before <stop>. The position of the '\0' is returned if the conversion
1237 * completes. If bytes are missing between <start> and <stop>, then the
1238 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
1239 * cannot even be stored so we return <start> without writing the 0.
1240 * The input string must also be zero-terminated.
1241 */
1242const char hextab[16] = "0123456789ABCDEF";
1243char *encode_string(char *start, char *stop,
1244 const char escape, const fd_set *map,
1245 const char *string)
1246{
1247 if (start < stop) {
1248 stop--; /* reserve one byte for the final '\0' */
1249 while (start < stop && *string != '\0') {
1250 if (!FD_ISSET((unsigned char)(*string), map))
1251 *start++ = *string;
1252 else {
1253 if (start + 3 >= stop)
1254 break;
1255 *start++ = escape;
1256 *start++ = hextab[(*string >> 4) & 15];
1257 *start++ = hextab[*string & 15];
1258 }
1259 string++;
1260 }
1261 *start = '\0';
1262 }
1263 return start;
1264}
1265
Thierry FOURNIERe059ec92014-03-17 12:01:13 +01001266/*
1267 * Same behavior as encode_string() above, except that it encodes chunk
1268 * <chunk> instead of a string.
1269 */
1270char *encode_chunk(char *start, char *stop,
1271 const char escape, const fd_set *map,
1272 const struct chunk *chunk)
1273{
1274 char *str = chunk->str;
1275 char *end = chunk->str + chunk->len;
1276
1277 if (start < stop) {
1278 stop--; /* reserve one byte for the final '\0' */
1279 while (start < stop && str < end) {
1280 if (!FD_ISSET((unsigned char)(*str), map))
1281 *start++ = *str;
1282 else {
1283 if (start + 3 >= stop)
1284 break;
1285 *start++ = escape;
1286 *start++ = hextab[(*str >> 4) & 15];
1287 *start++ = hextab[*str & 15];
1288 }
1289 str++;
1290 }
1291 *start = '\0';
1292 }
1293 return start;
1294}
1295
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001296/* Decode an URL-encoded string in-place. The resulting string might
1297 * be shorter. If some forbidden characters are found, the conversion is
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001298 * aborted, the string is truncated before the issue and a negative value is
1299 * returned, otherwise the operation returns the length of the decoded string.
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001300 */
1301int url_decode(char *string)
1302{
1303 char *in, *out;
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001304 int ret = -1;
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001305
1306 in = string;
1307 out = string;
1308 while (*in) {
1309 switch (*in) {
1310 case '+' :
1311 *out++ = ' ';
1312 break;
1313 case '%' :
1314 if (!ishex(in[1]) || !ishex(in[2]))
1315 goto end;
1316 *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]);
1317 in += 2;
1318 break;
1319 default:
1320 *out++ = *in;
1321 break;
1322 }
1323 in++;
1324 }
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001325 ret = out - string; /* success */
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001326 end:
1327 *out = 0;
1328 return ret;
1329}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001330
Willy Tarreau6911fa42007-03-04 18:06:08 +01001331unsigned int str2ui(const char *s)
1332{
1333 return __str2ui(s);
1334}
1335
1336unsigned int str2uic(const char *s)
1337{
1338 return __str2uic(s);
1339}
1340
1341unsigned int strl2ui(const char *s, int len)
1342{
1343 return __strl2ui(s, len);
1344}
1345
1346unsigned int strl2uic(const char *s, int len)
1347{
1348 return __strl2uic(s, len);
1349}
1350
Willy Tarreau4ec83cd2010-10-15 23:19:55 +02001351unsigned int read_uint(const char **s, const char *end)
1352{
1353 return __read_uint(s, end);
1354}
1355
Willy Tarreau6911fa42007-03-04 18:06:08 +01001356/* This one is 7 times faster than strtol() on athlon with checks.
1357 * It returns the value of the number composed of all valid digits read,
1358 * and can process negative numbers too.
1359 */
1360int strl2ic(const char *s, int len)
1361{
1362 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001363 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001364
1365 if (len > 0) {
1366 if (*s != '-') {
1367 /* positive number */
1368 while (len-- > 0) {
1369 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001370 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001371 if (j > 9)
1372 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001373 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001374 }
1375 } else {
1376 /* negative number */
1377 s++;
1378 while (--len > 0) {
1379 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001380 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001381 if (j > 9)
1382 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001383 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001384 }
1385 }
1386 }
1387 return i;
1388}
1389
1390
1391/* This function reads exactly <len> chars from <s> and converts them to a
1392 * signed integer which it stores into <ret>. It accurately detects any error
1393 * (truncated string, invalid chars, overflows). It is meant to be used in
1394 * applications designed for hostile environments. It returns zero when the
1395 * number has successfully been converted, non-zero otherwise. When an error
1396 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
1397 * faster than strtol().
1398 */
1399int strl2irc(const char *s, int len, int *ret)
1400{
1401 int i = 0;
1402 int j;
1403
1404 if (!len)
1405 return 1;
1406
1407 if (*s != '-') {
1408 /* positive number */
1409 while (len-- > 0) {
1410 j = (*s++) - '0';
1411 if (j > 9) return 1; /* invalid char */
1412 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
1413 i = i * 10;
1414 if (i + j < i) return 1; /* check for addition overflow */
1415 i = i + j;
1416 }
1417 } else {
1418 /* negative number */
1419 s++;
1420 while (--len > 0) {
1421 j = (*s++) - '0';
1422 if (j > 9) return 1; /* invalid char */
1423 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
1424 i = i * 10;
1425 if (i - j > i) return 1; /* check for subtract overflow */
1426 i = i - j;
1427 }
1428 }
1429 *ret = i;
1430 return 0;
1431}
1432
1433
1434/* This function reads exactly <len> chars from <s> and converts them to a
1435 * signed integer which it stores into <ret>. It accurately detects any error
1436 * (truncated string, invalid chars, overflows). It is meant to be used in
1437 * applications designed for hostile environments. It returns zero when the
1438 * number has successfully been converted, non-zero otherwise. When an error
1439 * is returned, the <ret> value is left untouched. It is about 3 times slower
1440 * than str2irc().
1441 */
Willy Tarreau6911fa42007-03-04 18:06:08 +01001442
1443int strl2llrc(const char *s, int len, long long *ret)
1444{
1445 long long i = 0;
1446 int j;
1447
1448 if (!len)
1449 return 1;
1450
1451 if (*s != '-') {
1452 /* positive number */
1453 while (len-- > 0) {
1454 j = (*s++) - '0';
1455 if (j > 9) return 1; /* invalid char */
1456 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
1457 i = i * 10LL;
1458 if (i + j < i) return 1; /* check for addition overflow */
1459 i = i + j;
1460 }
1461 } else {
1462 /* negative number */
1463 s++;
1464 while (--len > 0) {
1465 j = (*s++) - '0';
1466 if (j > 9) return 1; /* invalid char */
1467 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
1468 i = i * 10LL;
1469 if (i - j > i) return 1; /* check for subtract overflow */
1470 i = i - j;
1471 }
1472 }
1473 *ret = i;
1474 return 0;
1475}
1476
Thierry FOURNIER511e9472014-01-23 17:40:34 +01001477/* This function is used with pat_parse_dotted_ver(). It converts a string
1478 * composed by two number separated by a dot. Each part must contain in 16 bits
1479 * because internally they will be represented as a 32-bit quantity stored in
1480 * a 64-bit integer. It returns zero when the number has successfully been
1481 * converted, non-zero otherwise. When an error is returned, the <ret> value
1482 * is left untouched.
1483 *
1484 * "1.3" -> 0x0000000000010003
1485 * "65535.65535" -> 0x00000000ffffffff
1486 */
1487int strl2llrc_dotted(const char *text, int len, long long *ret)
1488{
1489 const char *end = &text[len];
1490 const char *p;
1491 long long major, minor;
1492
1493 /* Look for dot. */
1494 for (p = text; p < end; p++)
1495 if (*p == '.')
1496 break;
1497
1498 /* Convert major. */
1499 if (strl2llrc(text, p - text, &major) != 0)
1500 return 1;
1501
1502 /* Check major. */
1503 if (major >= 65536)
1504 return 1;
1505
1506 /* Convert minor. */
1507 minor = 0;
1508 if (p < end)
1509 if (strl2llrc(p + 1, end - (p + 1), &minor) != 0)
1510 return 1;
1511
1512 /* Check minor. */
1513 if (minor >= 65536)
1514 return 1;
1515
1516 /* Compose value. */
1517 *ret = (major << 16) | (minor & 0xffff);
1518 return 0;
1519}
1520
Willy Tarreaua0d37b62007-12-02 22:00:35 +01001521/* This function parses a time value optionally followed by a unit suffix among
1522 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
1523 * expected by the caller. The computation does its best to avoid overflows.
1524 * The value is returned in <ret> if everything is fine, and a NULL is returned
1525 * by the function. In case of error, a pointer to the error is returned and
1526 * <ret> is left untouched. Values are automatically rounded up when needed.
1527 */
1528const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
1529{
1530 unsigned imult, idiv;
1531 unsigned omult, odiv;
1532 unsigned value;
1533
1534 omult = odiv = 1;
1535
1536 switch (unit_flags & TIME_UNIT_MASK) {
1537 case TIME_UNIT_US: omult = 1000000; break;
1538 case TIME_UNIT_MS: omult = 1000; break;
1539 case TIME_UNIT_S: break;
1540 case TIME_UNIT_MIN: odiv = 60; break;
1541 case TIME_UNIT_HOUR: odiv = 3600; break;
1542 case TIME_UNIT_DAY: odiv = 86400; break;
1543 default: break;
1544 }
1545
1546 value = 0;
1547
1548 while (1) {
1549 unsigned int j;
1550
1551 j = *text - '0';
1552 if (j > 9)
1553 break;
1554 text++;
1555 value *= 10;
1556 value += j;
1557 }
1558
1559 imult = idiv = 1;
1560 switch (*text) {
1561 case '\0': /* no unit = default unit */
1562 imult = omult = idiv = odiv = 1;
1563 break;
1564 case 's': /* second = unscaled unit */
1565 break;
1566 case 'u': /* microsecond : "us" */
1567 if (text[1] == 's') {
1568 idiv = 1000000;
1569 text++;
1570 }
1571 break;
1572 case 'm': /* millisecond : "ms" or minute: "m" */
1573 if (text[1] == 's') {
1574 idiv = 1000;
1575 text++;
1576 } else
1577 imult = 60;
1578 break;
1579 case 'h': /* hour : "h" */
1580 imult = 3600;
1581 break;
1582 case 'd': /* day : "d" */
1583 imult = 86400;
1584 break;
1585 default:
1586 return text;
1587 break;
1588 }
1589
1590 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
1591 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
1592 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
1593 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
1594
1595 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
1596 *ret = value;
1597 return NULL;
1598}
Willy Tarreau6911fa42007-03-04 18:06:08 +01001599
Emeric Brun39132b22010-01-04 14:57:24 +01001600/* this function converts the string starting at <text> to an unsigned int
1601 * stored in <ret>. If an error is detected, the pointer to the unexpected
1602 * character is returned. If the conversio is succesful, NULL is returned.
1603 */
1604const char *parse_size_err(const char *text, unsigned *ret) {
1605 unsigned value = 0;
1606
1607 while (1) {
1608 unsigned int j;
1609
1610 j = *text - '0';
1611 if (j > 9)
1612 break;
1613 if (value > ~0U / 10)
1614 return text;
1615 value *= 10;
1616 if (value > (value + j))
1617 return text;
1618 value += j;
1619 text++;
1620 }
1621
1622 switch (*text) {
1623 case '\0':
1624 break;
1625 case 'K':
1626 case 'k':
1627 if (value > ~0U >> 10)
1628 return text;
1629 value = value << 10;
1630 break;
1631 case 'M':
1632 case 'm':
1633 if (value > ~0U >> 20)
1634 return text;
1635 value = value << 20;
1636 break;
1637 case 'G':
1638 case 'g':
1639 if (value > ~0U >> 30)
1640 return text;
1641 value = value << 30;
1642 break;
1643 default:
1644 return text;
1645 }
1646
1647 *ret = value;
1648 return NULL;
1649}
1650
Willy Tarreau126d4062013-12-03 17:50:47 +01001651/*
1652 * Parse binary string written in hexadecimal (source) and store the decoded
1653 * result into binstr and set binstrlen to the lengh of binstr. Memory for
1654 * binstr is allocated by the function. In case of error, returns 0 with an
Thierry FOURNIERee330af2014-01-21 11:36:14 +01001655 * error message in err. In succes case, it returns the consumed length.
Willy Tarreau126d4062013-12-03 17:50:47 +01001656 */
1657int parse_binary(const char *source, char **binstr, int *binstrlen, char **err)
1658{
1659 int len;
1660 const char *p = source;
1661 int i,j;
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001662 int alloc;
Willy Tarreau126d4062013-12-03 17:50:47 +01001663
1664 len = strlen(source);
1665 if (len % 2) {
1666 memprintf(err, "an even number of hex digit is expected");
1667 return 0;
1668 }
1669
1670 len = len >> 1;
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001671
Willy Tarreau126d4062013-12-03 17:50:47 +01001672 if (!*binstr) {
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001673 *binstr = calloc(len, sizeof(char));
1674 if (!*binstr) {
1675 memprintf(err, "out of memory while loading string pattern");
1676 return 0;
1677 }
1678 alloc = 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001679 }
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001680 else {
1681 if (*binstrlen < len) {
1682 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
1683 len, *binstrlen);
1684 return 0;
1685 }
1686 alloc = 0;
1687 }
1688 *binstrlen = len;
Willy Tarreau126d4062013-12-03 17:50:47 +01001689
1690 i = j = 0;
1691 while (j < len) {
1692 if (!ishex(p[i++]))
1693 goto bad_input;
1694 if (!ishex(p[i++]))
1695 goto bad_input;
1696 (*binstr)[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
1697 }
Thierry FOURNIERee330af2014-01-21 11:36:14 +01001698 return len << 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001699
1700bad_input:
1701 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001702 if (alloc)
1703 free(binstr);
Willy Tarreau126d4062013-12-03 17:50:47 +01001704 return 0;
1705}
1706
Willy Tarreau946ba592009-05-10 15:41:18 +02001707/* copies at most <n> characters from <src> and always terminates with '\0' */
1708char *my_strndup(const char *src, int n)
1709{
1710 int len = 0;
1711 char *ret;
1712
1713 while (len < n && src[len])
1714 len++;
1715
1716 ret = (char *)malloc(len + 1);
1717 if (!ret)
1718 return ret;
1719 memcpy(ret, src, len);
1720 ret[len] = '\0';
1721 return ret;
1722}
1723
Baptiste Assmannbb77c8e2013-10-06 23:24:13 +02001724/*
1725 * search needle in haystack
1726 * returns the pointer if found, returns NULL otherwise
1727 */
1728const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen)
1729{
1730 const void *c = NULL;
1731 unsigned char f;
1732
1733 if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen))
1734 return NULL;
1735
1736 f = *(char *)needle;
1737 c = haystack;
1738 while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) {
1739 if ((haystacklen - (c - haystack)) < needlelen)
1740 return NULL;
1741
1742 if (memcmp(c, needle, needlelen) == 0)
1743 return c;
1744 ++c;
1745 }
1746 return NULL;
1747}
1748
Willy Tarreau482b00d2009-10-04 22:48:42 +02001749/* This function returns the first unused key greater than or equal to <key> in
1750 * ID tree <root>. Zero is returned if no place is found.
1751 */
1752unsigned int get_next_id(struct eb_root *root, unsigned int key)
1753{
1754 struct eb32_node *used;
1755
1756 do {
1757 used = eb32_lookup_ge(root, key);
1758 if (!used || used->key > key)
1759 return key; /* key is available */
1760 key++;
1761 } while (key);
1762 return key;
1763}
1764
Willy Tarreau348238b2010-01-18 15:05:57 +01001765/* This function compares a sample word possibly followed by blanks to another
1766 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
1767 * otherwise zero. This intends to be used when checking HTTP headers for some
1768 * values. Note that it validates a word followed only by blanks but does not
1769 * validate a word followed by blanks then other chars.
1770 */
1771int word_match(const char *sample, int slen, const char *word, int wlen)
1772{
1773 if (slen < wlen)
1774 return 0;
1775
1776 while (wlen) {
1777 char c = *sample ^ *word;
1778 if (c && c != ('A' ^ 'a'))
1779 return 0;
1780 sample++;
1781 word++;
1782 slen--;
1783 wlen--;
1784 }
1785
1786 while (slen) {
1787 if (*sample != ' ' && *sample != '\t')
1788 return 0;
1789 sample++;
1790 slen--;
1791 }
1792 return 1;
1793}
Willy Tarreau482b00d2009-10-04 22:48:42 +02001794
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001795/* Converts any text-formatted IPv4 address to a host-order IPv4 address. It
1796 * is particularly fast because it avoids expensive operations such as
1797 * multiplies, which are optimized away at the end. It requires a properly
1798 * formated address though (3 points).
1799 */
1800unsigned int inetaddr_host(const char *text)
1801{
1802 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1803 register unsigned int dig100, dig10, dig1;
1804 int s;
1805 const char *p, *d;
1806
1807 dig1 = dig10 = dig100 = ascii_zero;
1808 s = 24;
1809
1810 p = text;
1811 while (1) {
1812 if (((unsigned)(*p - '0')) <= 9) {
1813 p++;
1814 continue;
1815 }
1816
1817 /* here, we have a complete byte between <text> and <p> (exclusive) */
1818 if (p == text)
1819 goto end;
1820
1821 d = p - 1;
1822 dig1 |= (unsigned int)(*d << s);
1823 if (d == text)
1824 goto end;
1825
1826 d--;
1827 dig10 |= (unsigned int)(*d << s);
1828 if (d == text)
1829 goto end;
1830
1831 d--;
1832 dig100 |= (unsigned int)(*d << s);
1833 end:
1834 if (!s || *p != '.')
1835 break;
1836
1837 s -= 8;
1838 text = ++p;
1839 }
1840
1841 dig100 -= ascii_zero;
1842 dig10 -= ascii_zero;
1843 dig1 -= ascii_zero;
1844 return ((dig100 * 10) + dig10) * 10 + dig1;
1845}
1846
1847/*
1848 * Idem except the first unparsed character has to be passed in <stop>.
1849 */
1850unsigned int inetaddr_host_lim(const char *text, const char *stop)
1851{
1852 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1853 register unsigned int dig100, dig10, dig1;
1854 int s;
1855 const char *p, *d;
1856
1857 dig1 = dig10 = dig100 = ascii_zero;
1858 s = 24;
1859
1860 p = text;
1861 while (1) {
1862 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1863 p++;
1864 continue;
1865 }
1866
1867 /* here, we have a complete byte between <text> and <p> (exclusive) */
1868 if (p == text)
1869 goto end;
1870
1871 d = p - 1;
1872 dig1 |= (unsigned int)(*d << s);
1873 if (d == text)
1874 goto end;
1875
1876 d--;
1877 dig10 |= (unsigned int)(*d << s);
1878 if (d == text)
1879 goto end;
1880
1881 d--;
1882 dig100 |= (unsigned int)(*d << s);
1883 end:
1884 if (!s || p == stop || *p != '.')
1885 break;
1886
1887 s -= 8;
1888 text = ++p;
1889 }
1890
1891 dig100 -= ascii_zero;
1892 dig10 -= ascii_zero;
1893 dig1 -= ascii_zero;
1894 return ((dig100 * 10) + dig10) * 10 + dig1;
1895}
1896
1897/*
1898 * Idem except the pointer to first unparsed byte is returned into <ret> which
1899 * must not be NULL.
1900 */
Willy Tarreau74172752010-10-15 23:21:42 +02001901unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret)
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001902{
1903 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1904 register unsigned int dig100, dig10, dig1;
1905 int s;
Willy Tarreau74172752010-10-15 23:21:42 +02001906 char *p, *d;
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001907
1908 dig1 = dig10 = dig100 = ascii_zero;
1909 s = 24;
1910
1911 p = text;
1912 while (1) {
1913 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1914 p++;
1915 continue;
1916 }
1917
1918 /* here, we have a complete byte between <text> and <p> (exclusive) */
1919 if (p == text)
1920 goto end;
1921
1922 d = p - 1;
1923 dig1 |= (unsigned int)(*d << s);
1924 if (d == text)
1925 goto end;
1926
1927 d--;
1928 dig10 |= (unsigned int)(*d << s);
1929 if (d == text)
1930 goto end;
1931
1932 d--;
1933 dig100 |= (unsigned int)(*d << s);
1934 end:
1935 if (!s || p == stop || *p != '.')
1936 break;
1937
1938 s -= 8;
1939 text = ++p;
1940 }
1941
1942 *ret = p;
1943 dig100 -= ascii_zero;
1944 dig10 -= ascii_zero;
1945 dig1 -= ascii_zero;
1946 return ((dig100 * 10) + dig10) * 10 + dig1;
1947}
1948
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001949/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
1950 * or the number of chars read in case of success. Maybe this could be replaced
1951 * by one of the functions above. Also, apparently this function does not support
1952 * hosts above 255 and requires exactly 4 octets.
Willy Tarreau075415a2013-12-12 11:29:39 +01001953 * The destination is only modified on success.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001954 */
1955int buf2ip(const char *buf, size_t len, struct in_addr *dst)
1956{
1957 const char *addr;
1958 int saw_digit, octets, ch;
1959 u_char tmp[4], *tp;
1960 const char *cp = buf;
1961
1962 saw_digit = 0;
1963 octets = 0;
1964 *(tp = tmp) = 0;
1965
1966 for (addr = buf; addr - buf < len; addr++) {
1967 unsigned char digit = (ch = *addr) - '0';
1968
1969 if (digit > 9 && ch != '.')
1970 break;
1971
1972 if (digit <= 9) {
1973 u_int new = *tp * 10 + digit;
1974
1975 if (new > 255)
1976 return 0;
1977
1978 *tp = new;
1979
1980 if (!saw_digit) {
1981 if (++octets > 4)
1982 return 0;
1983 saw_digit = 1;
1984 }
1985 } else if (ch == '.' && saw_digit) {
1986 if (octets == 4)
1987 return 0;
1988
1989 *++tp = 0;
1990 saw_digit = 0;
1991 } else
1992 return 0;
1993 }
1994
1995 if (octets < 4)
1996 return 0;
1997
1998 memcpy(&dst->s_addr, tmp, 4);
1999 return addr - cp;
2000}
2001
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002002/* This function converts the string in <buf> of the len <len> to
2003 * struct in6_addr <dst> which must be allocated by the caller.
2004 * This function returns 1 in success case, otherwise zero.
Willy Tarreau075415a2013-12-12 11:29:39 +01002005 * The destination is only modified on success.
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002006 */
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002007int buf2ip6(const char *buf, size_t len, struct in6_addr *dst)
2008{
Thierry FOURNIERcd659912013-12-11 12:33:54 +01002009 char null_term_ip6[INET6_ADDRSTRLEN + 1];
Willy Tarreau075415a2013-12-12 11:29:39 +01002010 struct in6_addr out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002011
Thierry FOURNIERcd659912013-12-11 12:33:54 +01002012 if (len > INET6_ADDRSTRLEN)
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002013 return 0;
2014
2015 memcpy(null_term_ip6, buf, len);
2016 null_term_ip6[len] = '\0';
2017
Willy Tarreau075415a2013-12-12 11:29:39 +01002018 if (!inet_pton(AF_INET6, null_term_ip6, &out))
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002019 return 0;
2020
Willy Tarreau075415a2013-12-12 11:29:39 +01002021 *dst = out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01002022 return 1;
2023}
2024
Willy Tarreauacf95772010-06-14 19:09:21 +02002025/* To be used to quote config arg positions. Returns the short string at <ptr>
2026 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
2027 * if ptr is NULL or empty. The string is locally allocated.
2028 */
2029const char *quote_arg(const char *ptr)
2030{
2031 static char val[32];
2032 int i;
2033
2034 if (!ptr || !*ptr)
2035 return "end of line";
2036 val[0] = '\'';
Willy Tarreaude2dd6b2013-01-24 02:14:42 +01002037 for (i = 1; i < sizeof(val) - 2 && *ptr; i++)
Willy Tarreauacf95772010-06-14 19:09:21 +02002038 val[i] = *ptr++;
2039 val[i++] = '\'';
2040 val[i] = '\0';
2041 return val;
2042}
2043
Willy Tarreau5b180202010-07-18 10:40:48 +02002044/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
2045int get_std_op(const char *str)
2046{
2047 int ret = -1;
2048
2049 if (*str == 'e' && str[1] == 'q')
2050 ret = STD_OP_EQ;
2051 else if (*str == 'n' && str[1] == 'e')
2052 ret = STD_OP_NE;
2053 else if (*str == 'l') {
2054 if (str[1] == 'e') ret = STD_OP_LE;
2055 else if (str[1] == 't') ret = STD_OP_LT;
2056 }
2057 else if (*str == 'g') {
2058 if (str[1] == 'e') ret = STD_OP_GE;
2059 else if (str[1] == 't') ret = STD_OP_GT;
2060 }
2061
2062 if (ret == -1 || str[2] != '\0')
2063 return -1;
2064 return ret;
2065}
2066
Willy Tarreau4c14eaa2010-11-24 14:01:45 +01002067/* hash a 32-bit integer to another 32-bit integer */
2068unsigned int full_hash(unsigned int a)
2069{
2070 return __full_hash(a);
2071}
2072
David du Colombier4f92d322011-03-24 11:09:31 +01002073/* Return non-zero if IPv4 address is part of the network,
2074 * otherwise zero.
2075 */
2076int in_net_ipv4(struct in_addr *addr, struct in_addr *mask, struct in_addr *net)
2077{
2078 return((addr->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr));
2079}
2080
2081/* Return non-zero if IPv6 address is part of the network,
2082 * otherwise zero.
2083 */
2084int in_net_ipv6(struct in6_addr *addr, struct in6_addr *mask, struct in6_addr *net)
2085{
2086 int i;
2087
2088 for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++)
2089 if (((((int *)addr)[i] & ((int *)mask)[i])) !=
2090 (((int *)net)[i] & ((int *)mask)[i]))
2091 return 0;
2092 return 1;
2093}
2094
2095/* RFC 4291 prefix */
2096const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00,
2097 0x00, 0x00, 0x00, 0x00,
2098 0x00, 0x00, 0xFF, 0xFF };
2099
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002100/* Map IPv4 adress on IPv6 address, as specified in RFC 3513.
2101 * Input and output may overlap.
2102 */
David du Colombier4f92d322011-03-24 11:09:31 +01002103void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr)
2104{
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002105 struct in_addr tmp_addr;
2106
2107 tmp_addr.s_addr = sin_addr->s_addr;
David du Colombier4f92d322011-03-24 11:09:31 +01002108 memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx));
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002109 memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4);
David du Colombier4f92d322011-03-24 11:09:31 +01002110}
2111
2112/* Map IPv6 adress on IPv4 address, as specified in RFC 3513.
2113 * Return true if conversion is possible and false otherwise.
2114 */
2115int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr)
2116{
2117 if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) {
2118 memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]),
2119 sizeof(struct in_addr));
2120 return 1;
2121 }
2122
2123 return 0;
2124}
2125
William Lallemand421f5b52012-02-06 18:15:57 +01002126char *human_time(int t, short hz_div) {
2127 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
2128 char *p = rv;
Willy Tarreau761b3d52014-04-14 14:53:06 +02002129 char *end = rv + sizeof(rv);
William Lallemand421f5b52012-02-06 18:15:57 +01002130 int cnt=2; // print two numbers
2131
2132 if (unlikely(t < 0 || hz_div <= 0)) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002133 snprintf(p, end - p, "?");
William Lallemand421f5b52012-02-06 18:15:57 +01002134 return rv;
2135 }
2136
2137 if (unlikely(hz_div > 1))
2138 t /= hz_div;
2139
2140 if (t >= DAY) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002141 p += snprintf(p, end - p, "%dd", t / DAY);
William Lallemand421f5b52012-02-06 18:15:57 +01002142 cnt--;
2143 }
2144
2145 if (cnt && t % DAY / HOUR) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002146 p += snprintf(p, end - p, "%dh", t % DAY / HOUR);
William Lallemand421f5b52012-02-06 18:15:57 +01002147 cnt--;
2148 }
2149
2150 if (cnt && t % HOUR / MINUTE) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002151 p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE);
William Lallemand421f5b52012-02-06 18:15:57 +01002152 cnt--;
2153 }
2154
2155 if ((cnt && t % MINUTE) || !t) // also display '0s'
Willy Tarreau761b3d52014-04-14 14:53:06 +02002156 p += snprintf(p, end - p, "%ds", t % MINUTE / SEC);
William Lallemand421f5b52012-02-06 18:15:57 +01002157
2158 return rv;
2159}
2160
2161const char *monthname[12] = {
2162 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2163 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2164};
2165
2166/* date2str_log: write a date in the format :
2167 * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d",
2168 * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2169 * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000);
2170 *
2171 * without using sprintf. return a pointer to the last char written (\0) or
2172 * NULL if there isn't enough space.
2173 */
2174char *date2str_log(char *dst, struct tm *tm, struct timeval *date, size_t size)
2175{
2176
2177 if (size < 25) /* the size is fixed: 24 chars + \0 */
2178 return NULL;
2179
2180 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2181 *dst++ = '/';
2182 memcpy(dst, monthname[tm->tm_mon], 3); // month
2183 dst += 3;
2184 *dst++ = '/';
2185 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2186 *dst++ = ':';
2187 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2188 *dst++ = ':';
2189 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2190 *dst++ = ':';
2191 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2192 *dst++ = '.';
2193 utoa_pad((unsigned int)(date->tv_usec/1000), dst, 4); // millisecondes
2194 dst += 3; // only the 3 first digits
2195 *dst = '\0';
2196
2197 return dst;
2198}
2199
2200/* gmt2str_log: write a date in the format :
2201 * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf
2202 * return a pointer to the last char written (\0) or
2203 * NULL if there isn't enough space.
2204 */
2205char *gmt2str_log(char *dst, struct tm *tm, size_t size)
2206{
Yuxans Yao4e25b012012-10-19 10:36:09 +08002207 if (size < 27) /* the size is fixed: 26 chars + \0 */
William Lallemand421f5b52012-02-06 18:15:57 +01002208 return NULL;
2209
2210 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2211 *dst++ = '/';
2212 memcpy(dst, monthname[tm->tm_mon], 3); // month
2213 dst += 3;
2214 *dst++ = '/';
2215 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2216 *dst++ = ':';
2217 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2218 *dst++ = ':';
2219 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2220 *dst++ = ':';
2221 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2222 *dst++ = ' ';
2223 *dst++ = '+';
2224 *dst++ = '0';
2225 *dst++ = '0';
2226 *dst++ = '0';
2227 *dst++ = '0';
2228 *dst = '\0';
2229
2230 return dst;
2231}
2232
Yuxans Yao4e25b012012-10-19 10:36:09 +08002233/* localdate2str_log: write a date in the format :
2234 * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf
2235 * * return a pointer to the last char written (\0) or
2236 * * NULL if there isn't enough space.
2237 */
2238char *localdate2str_log(char *dst, struct tm *tm, size_t size)
2239{
2240 if (size < 27) /* the size is fixed: 26 chars + \0 */
2241 return NULL;
2242
2243 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2244 *dst++ = '/';
2245 memcpy(dst, monthname[tm->tm_mon], 3); // month
2246 dst += 3;
2247 *dst++ = '/';
2248 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2249 *dst++ = ':';
2250 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2251 *dst++ = ':';
2252 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2253 *dst++ = ':';
2254 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2255 *dst++ = ' ';
2256 memcpy(dst, localtimezone, 5); // timezone
2257 dst += 5;
2258 *dst = '\0';
2259
2260 return dst;
2261}
2262
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002263/* Dynamically allocates a string of the proper length to hold the formatted
2264 * output. NULL is returned on error. The caller is responsible for freeing the
2265 * memory area using free(). The resulting string is returned in <out> if the
2266 * pointer is not NULL. A previous version of <out> might be used to build the
2267 * new string, and it will be freed before returning if it is not NULL, which
2268 * makes it possible to build complex strings from iterative calls without
2269 * having to care about freeing intermediate values, as in the example below :
2270 *
2271 * memprintf(&err, "invalid argument: '%s'", arg);
2272 * ...
2273 * memprintf(&err, "parser said : <%s>\n", *err);
2274 * ...
2275 * free(*err);
2276 *
2277 * This means that <err> must be initialized to NULL before first invocation.
2278 * The return value also holds the allocated string, which eases error checking
2279 * and immediate consumption. If the output pointer is not used, NULL must be
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002280 * passed instead and it will be ignored. The returned message will then also
2281 * be NULL so that the caller does not have to bother with freeing anything.
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002282 *
2283 * It is also convenient to use it without any free except the last one :
2284 * err = NULL;
2285 * if (!fct1(err)) report(*err);
2286 * if (!fct2(err)) report(*err);
2287 * if (!fct3(err)) report(*err);
2288 * free(*err);
2289 */
2290char *memprintf(char **out, const char *format, ...)
2291{
2292 va_list args;
2293 char *ret = NULL;
2294 int allocated = 0;
2295 int needed = 0;
2296
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002297 if (!out)
2298 return NULL;
2299
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002300 do {
2301 /* vsnprintf() will return the required length even when the
2302 * target buffer is NULL. We do this in a loop just in case
2303 * intermediate evaluations get wrong.
2304 */
2305 va_start(args, format);
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002306 needed = vsnprintf(ret, allocated, format, args);
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002307 va_end(args);
2308
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002309 if (needed < allocated) {
2310 /* Note: on Solaris 8, the first iteration always
2311 * returns -1 if allocated is zero, so we force a
2312 * retry.
2313 */
2314 if (!allocated)
2315 needed = 0;
2316 else
2317 break;
2318 }
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002319
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002320 allocated = needed + 1;
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002321 ret = realloc(ret, allocated);
2322 } while (ret);
2323
2324 if (needed < 0) {
2325 /* an error was encountered */
2326 free(ret);
2327 ret = NULL;
2328 }
2329
2330 if (out) {
2331 free(*out);
2332 *out = ret;
2333 }
2334
2335 return ret;
2336}
William Lallemand421f5b52012-02-06 18:15:57 +01002337
Willy Tarreau21c705b2012-09-14 11:40:36 +02002338/* Used to add <level> spaces before each line of <out>, unless there is only one line.
2339 * The input argument is automatically freed and reassigned. The result will have to be
Willy Tarreau70eec382012-10-10 08:56:47 +02002340 * freed by the caller. It also supports being passed a NULL which results in the same
2341 * output.
Willy Tarreau21c705b2012-09-14 11:40:36 +02002342 * Example of use :
2343 * parse(cmd, &err); (callee: memprintf(&err, ...))
2344 * fprintf(stderr, "Parser said: %s\n", indent_error(&err));
2345 * free(err);
2346 */
2347char *indent_msg(char **out, int level)
2348{
2349 char *ret, *in, *p;
2350 int needed = 0;
2351 int lf = 0;
2352 int lastlf = 0;
2353 int len;
2354
Willy Tarreau70eec382012-10-10 08:56:47 +02002355 if (!out || !*out)
2356 return NULL;
2357
Willy Tarreau21c705b2012-09-14 11:40:36 +02002358 in = *out - 1;
2359 while ((in = strchr(in + 1, '\n')) != NULL) {
2360 lastlf = in - *out;
2361 lf++;
2362 }
2363
2364 if (!lf) /* single line, no LF, return it as-is */
2365 return *out;
2366
2367 len = strlen(*out);
2368
2369 if (lf == 1 && lastlf == len - 1) {
2370 /* single line, LF at end, strip it and return as-is */
2371 (*out)[lastlf] = 0;
2372 return *out;
2373 }
2374
2375 /* OK now we have at least one LF, we need to process the whole string
2376 * as a multi-line string. What we'll do :
2377 * - prefix with an LF if there is none
2378 * - add <level> spaces before each line
2379 * This means at most ( 1 + level + (len-lf) + lf*<1+level) ) =
2380 * 1 + level + len + lf * level = 1 + level * (lf + 1) + len.
2381 */
2382
2383 needed = 1 + level * (lf + 1) + len + 1;
2384 p = ret = malloc(needed);
2385 in = *out;
2386
2387 /* skip initial LFs */
2388 while (*in == '\n')
2389 in++;
2390
2391 /* copy each line, prefixed with LF and <level> spaces, and without the trailing LF */
2392 while (*in) {
2393 *p++ = '\n';
2394 memset(p, ' ', level);
2395 p += level;
2396 do {
2397 *p++ = *in++;
2398 } while (*in && *in != '\n');
2399 if (*in)
2400 in++;
2401 }
2402 *p = 0;
2403
2404 free(*out);
2405 *out = ret;
2406
2407 return ret;
2408}
2409
Willy Tarreaudad36a32013-03-11 01:20:04 +01002410/* Convert occurrences of environment variables in the input string to their
2411 * corresponding value. A variable is identified as a series of alphanumeric
2412 * characters or underscores following a '$' sign. The <in> string must be
2413 * free()able. NULL returns NULL. The resulting string might be reallocated if
2414 * some expansion is made. Variable names may also be enclosed into braces if
2415 * needed (eg: to concatenate alphanum characters).
2416 */
2417char *env_expand(char *in)
2418{
2419 char *txt_beg;
2420 char *out;
2421 char *txt_end;
2422 char *var_beg;
2423 char *var_end;
2424 char *value;
2425 char *next;
2426 int out_len;
2427 int val_len;
2428
2429 if (!in)
2430 return in;
2431
2432 value = out = NULL;
2433 out_len = 0;
2434
2435 txt_beg = in;
2436 do {
2437 /* look for next '$' sign in <in> */
2438 for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++);
2439
2440 if (!*txt_end && !out) /* end and no expansion performed */
2441 return in;
2442
2443 val_len = 0;
2444 next = txt_end;
2445 if (*txt_end == '$') {
2446 char save;
2447
2448 var_beg = txt_end + 1;
2449 if (*var_beg == '{')
2450 var_beg++;
2451
2452 var_end = var_beg;
2453 while (isalnum((int)(unsigned char)*var_end) || *var_end == '_') {
2454 var_end++;
2455 }
2456
2457 next = var_end;
2458 if (*var_end == '}' && (var_beg > txt_end + 1))
2459 next++;
2460
2461 /* get value of the variable name at this location */
2462 save = *var_end;
2463 *var_end = '\0';
2464 value = getenv(var_beg);
2465 *var_end = save;
2466 val_len = value ? strlen(value) : 0;
2467 }
2468
2469 out = realloc(out, out_len + (txt_end - txt_beg) + val_len + 1);
2470 if (txt_end > txt_beg) {
2471 memcpy(out + out_len, txt_beg, txt_end - txt_beg);
2472 out_len += txt_end - txt_beg;
2473 }
2474 if (val_len) {
2475 memcpy(out + out_len, value, val_len);
2476 out_len += val_len;
2477 }
2478 out[out_len] = 0;
2479 txt_beg = next;
2480 } while (*txt_beg);
2481
2482 /* here we know that <out> was allocated and that we don't need <in> anymore */
2483 free(in);
2484 return out;
2485}
2486
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002487
2488/* same as strstr() but case-insensitive and with limit length */
2489const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2)
2490{
2491 char *pptr, *sptr, *start;
Willy Tarreauc8746532014-05-28 23:05:07 +02002492 unsigned int slen, plen;
2493 unsigned int tmp1, tmp2;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002494
2495 if (str1 == NULL || len_str1 == 0) // search pattern into an empty string => search is not found
2496 return NULL;
2497
2498 if (str2 == NULL || len_str2 == 0) // pattern is empty => every str1 match
2499 return str1;
2500
2501 if (len_str1 < len_str2) // pattern is longer than string => search is not found
2502 return NULL;
2503
2504 for (tmp1 = 0, start = (char *)str1, pptr = (char *)str2, slen = len_str1, plen = len_str2; slen >= plen; start++, slen--) {
2505 while (toupper(*start) != toupper(*str2)) {
2506 start++;
2507 slen--;
2508 tmp1++;
2509
2510 if (tmp1 >= len_str1)
2511 return NULL;
2512
2513 /* if pattern longer than string */
2514 if (slen < plen)
2515 return NULL;
2516 }
2517
2518 sptr = start;
2519 pptr = (char *)str2;
2520
2521 tmp2 = 0;
2522 while (toupper(*sptr) == toupper(*pptr)) {
2523 sptr++;
2524 pptr++;
2525 tmp2++;
2526
2527 if (*pptr == '\0' || tmp2 == len_str2) /* end of pattern found */
2528 return start;
2529 if (*sptr == '\0' || tmp2 == len_str1) /* end of string found and the pattern is not fully found */
2530 return NULL;
2531 }
2532 }
2533 return NULL;
2534}
2535
Thierry FOURNIER317e1c42014-08-12 10:20:47 +02002536/* This function read the next valid utf8 char.
2537 * <s> is the byte srray to be decode, <len> is its length.
2538 * The function returns decoded char encoded like this:
2539 * The 4 msb are the return code (UTF8_CODE_*), the 4 lsb
2540 * are the length read. The decoded character is stored in <c>.
2541 */
2542unsigned char utf8_next(const char *s, int len, unsigned int *c)
2543{
2544 const unsigned char *p = (unsigned char *)s;
2545 int dec;
2546 unsigned char code = UTF8_CODE_OK;
2547
2548 if (len < 1)
2549 return UTF8_CODE_OK;
2550
2551 /* Check the type of UTF8 sequence
2552 *
2553 * 0... .... 0x00 <= x <= 0x7f : 1 byte: ascii char
2554 * 10.. .... 0x80 <= x <= 0xbf : invalid sequence
2555 * 110. .... 0xc0 <= x <= 0xdf : 2 bytes
2556 * 1110 .... 0xe0 <= x <= 0xef : 3 bytes
2557 * 1111 0... 0xf0 <= x <= 0xf7 : 4 bytes
2558 * 1111 10.. 0xf8 <= x <= 0xfb : 5 bytes
2559 * 1111 110. 0xfc <= x <= 0xfd : 6 bytes
2560 * 1111 111. 0xfe <= x <= 0xff : invalid sequence
2561 */
2562 switch (*p) {
2563 case 0x00 ... 0x7f:
2564 *c = *p;
2565 return UTF8_CODE_OK | 1;
2566
2567 case 0x80 ... 0xbf:
2568 *c = *p;
2569 return UTF8_CODE_BADSEQ | 1;
2570
2571 case 0xc0 ... 0xdf:
2572 if (len < 2) {
2573 *c = *p;
2574 return UTF8_CODE_BADSEQ | 1;
2575 }
2576 *c = *p & 0x1f;
2577 dec = 1;
2578 break;
2579
2580 case 0xe0 ... 0xef:
2581 if (len < 3) {
2582 *c = *p;
2583 return UTF8_CODE_BADSEQ | 1;
2584 }
2585 *c = *p & 0x0f;
2586 dec = 2;
2587 break;
2588
2589 case 0xf0 ... 0xf7:
2590 if (len < 4) {
2591 *c = *p;
2592 return UTF8_CODE_BADSEQ | 1;
2593 }
2594 *c = *p & 0x07;
2595 dec = 3;
2596 break;
2597
2598 case 0xf8 ... 0xfb:
2599 if (len < 5) {
2600 *c = *p;
2601 return UTF8_CODE_BADSEQ | 1;
2602 }
2603 *c = *p & 0x03;
2604 dec = 4;
2605 break;
2606
2607 case 0xfc ... 0xfd:
2608 if (len < 6) {
2609 *c = *p;
2610 return UTF8_CODE_BADSEQ | 1;
2611 }
2612 *c = *p & 0x01;
2613 dec = 5;
2614 break;
2615
2616 case 0xfe ... 0xff:
2617 default:
2618 *c = *p;
2619 return UTF8_CODE_BADSEQ | 1;
2620 }
2621
2622 p++;
2623
2624 while (dec > 0) {
2625
2626 /* need 0x10 for the 2 first bits */
2627 if ( ( *p & 0xc0 ) != 0x80 )
2628 return UTF8_CODE_BADSEQ | ((p-(unsigned char *)s)&0xffff);
2629
2630 /* add data at char */
2631 *c = ( *c << 6 ) | ( *p & 0x3f );
2632
2633 dec--;
2634 p++;
2635 }
2636
2637 /* Check ovelong encoding.
2638 * 1 byte : 5 + 6 : 11 : 0x80 ... 0x7ff
2639 * 2 bytes : 4 + 6 + 6 : 16 : 0x800 ... 0xffff
2640 * 3 bytes : 3 + 6 + 6 + 6 : 21 : 0x10000 ... 0x1fffff
2641 */
2642 if ((*c >= 0x00 && *c <= 0x7f && (p-(unsigned char *)s) > 1) ||
2643 (*c >= 0x80 && *c <= 0x7ff && (p-(unsigned char *)s) > 2) ||
2644 (*c >= 0x800 && *c <= 0xffff && (p-(unsigned char *)s) > 3) ||
2645 (*c >= 0x10000 && *c <= 0x1fffff && (p-(unsigned char *)s) > 4))
2646 code |= UTF8_CODE_OVERLONG;
2647
2648 /* Check invalid UTF8 range. */
2649 if ((*c >= 0xd800 && *c <= 0xdfff) ||
2650 (*c >= 0xfffe && *c <= 0xffff))
2651 code |= UTF8_CODE_INVRANGE;
2652
2653 return code | ((p-(unsigned char *)s)&0x0f);
2654}
2655
Willy Tarreaubaaee002006-06-26 02:48:02 +02002656/*
2657 * Local variables:
2658 * c-indent-level: 8
2659 * c-basic-offset: 8
2660 * End:
2661 */