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