blob: 06176d70866976b7c7324e4794802f8dcff4f40c [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
41/*
William Lallemande7340ec2012-01-24 11:15:39 +010042 * unsigned long long ASCII representation
43 *
44 * return the last char '\0' or NULL if no enough
45 * space in dst
46 */
47char *ulltoa(unsigned long long n, char *dst, size_t size)
48{
49 int i = 0;
50 char *res;
51
52 switch(n) {
53 case 1ULL ... 9ULL:
54 i = 0;
55 break;
56
57 case 10ULL ... 99ULL:
58 i = 1;
59 break;
60
61 case 100ULL ... 999ULL:
62 i = 2;
63 break;
64
65 case 1000ULL ... 9999ULL:
66 i = 3;
67 break;
68
69 case 10000ULL ... 99999ULL:
70 i = 4;
71 break;
72
73 case 100000ULL ... 999999ULL:
74 i = 5;
75 break;
76
77 case 1000000ULL ... 9999999ULL:
78 i = 6;
79 break;
80
81 case 10000000ULL ... 99999999ULL:
82 i = 7;
83 break;
84
85 case 100000000ULL ... 999999999ULL:
86 i = 8;
87 break;
88
89 case 1000000000ULL ... 9999999999ULL:
90 i = 9;
91 break;
92
93 case 10000000000ULL ... 99999999999ULL:
94 i = 10;
95 break;
96
97 case 100000000000ULL ... 999999999999ULL:
98 i = 11;
99 break;
100
101 case 1000000000000ULL ... 9999999999999ULL:
102 i = 12;
103 break;
104
105 case 10000000000000ULL ... 99999999999999ULL:
106 i = 13;
107 break;
108
109 case 100000000000000ULL ... 999999999999999ULL:
110 i = 14;
111 break;
112
113 case 1000000000000000ULL ... 9999999999999999ULL:
114 i = 15;
115 break;
116
117 case 10000000000000000ULL ... 99999999999999999ULL:
118 i = 16;
119 break;
120
121 case 100000000000000000ULL ... 999999999999999999ULL:
122 i = 17;
123 break;
124
125 case 1000000000000000000ULL ... 9999999999999999999ULL:
126 i = 18;
127 break;
128
129 case 10000000000000000000ULL ... ULLONG_MAX:
130 i = 19;
131 break;
132 }
133 if (i + 2 > size) // (i + 1) + '\0'
134 return NULL; // too long
135 res = dst + i + 1;
136 *res = '\0';
137 for (; i >= 0; i--) {
138 dst[i] = n % 10ULL + '0';
139 n /= 10ULL;
140 }
141 return res;
142}
143
144/*
145 * unsigned long ASCII representation
146 *
147 * return the last char '\0' or NULL if no enough
148 * space in dst
149 */
150char *ultoa_o(unsigned long n, char *dst, size_t size)
151{
152 int i = 0;
153 char *res;
154
155 switch (n) {
156 case 0U ... 9UL:
157 i = 0;
158 break;
159
160 case 10U ... 99UL:
161 i = 1;
162 break;
163
164 case 100U ... 999UL:
165 i = 2;
166 break;
167
168 case 1000U ... 9999UL:
169 i = 3;
170 break;
171
172 case 10000U ... 99999UL:
173 i = 4;
174 break;
175
176 case 100000U ... 999999UL:
177 i = 5;
178 break;
179
180 case 1000000U ... 9999999UL:
181 i = 6;
182 break;
183
184 case 10000000U ... 99999999UL:
185 i = 7;
186 break;
187
188 case 100000000U ... 999999999UL:
189 i = 8;
190 break;
191#if __WORDSIZE == 32
192
193 case 1000000000ULL ... ULONG_MAX:
194 i = 9;
195 break;
196
197#elif __WORDSIZE == 64
198
199 case 1000000000ULL ... 9999999999UL:
200 i = 9;
201 break;
202
203 case 10000000000ULL ... 99999999999UL:
204 i = 10;
205 break;
206
207 case 100000000000ULL ... 999999999999UL:
208 i = 11;
209 break;
210
211 case 1000000000000ULL ... 9999999999999UL:
212 i = 12;
213 break;
214
215 case 10000000000000ULL ... 99999999999999UL:
216 i = 13;
217 break;
218
219 case 100000000000000ULL ... 999999999999999UL:
220 i = 14;
221 break;
222
223 case 1000000000000000ULL ... 9999999999999999UL:
224 i = 15;
225 break;
226
227 case 10000000000000000ULL ... 99999999999999999UL:
228 i = 16;
229 break;
230
231 case 100000000000000000ULL ... 999999999999999999UL:
232 i = 17;
233 break;
234
235 case 1000000000000000000ULL ... 9999999999999999999UL:
236 i = 18;
237 break;
238
239 case 10000000000000000000ULL ... ULONG_MAX:
240 i = 19;
241 break;
242
243#endif
244 }
245 if (i + 2 > size) // (i + 1) + '\0'
246 return NULL; // too long
247 res = dst + i + 1;
248 *res = '\0';
249 for (; i >= 0; i--) {
250 dst[i] = n % 10U + '0';
251 n /= 10U;
252 }
253 return res;
254}
255
256/*
257 * signed long ASCII representation
258 *
259 * return the last char '\0' or NULL if no enough
260 * space in dst
261 */
262char *ltoa_o(long int n, char *dst, size_t size)
263{
264 char *pos = dst;
265
266 if (n < 0) {
267 if (size < 3)
268 return NULL; // min size is '-' + digit + '\0' but another test in ultoa
269 *pos = '-';
270 pos++;
271 dst = ultoa_o(-n, pos, size - 1);
272 } else {
273 dst = ultoa_o(n, dst, size);
274 }
275 return dst;
276}
277
278/*
279 * signed long long ASCII representation
280 *
281 * return the last char '\0' or NULL if no enough
282 * space in dst
283 */
284char *lltoa(long long n, char *dst, size_t size)
285{
286 char *pos = dst;
287
288 if (n < 0) {
289 if (size < 3)
290 return NULL; // min size is '-' + digit + '\0' but another test in ulltoa
291 *pos = '-';
292 pos++;
293 dst = ulltoa(-n, pos, size - 1);
294 } else {
295 dst = ulltoa(n, dst, size);
296 }
297 return dst;
298}
299
300/*
301 * write a ascii representation of a unsigned into dst,
302 * return a pointer to the last character
303 * Pad the ascii representation with '0', using size.
304 */
305char *utoa_pad(unsigned int n, char *dst, size_t size)
306{
307 int i = 0;
308 char *ret;
309
310 switch(n) {
311 case 0U ... 9U:
312 i = 0;
313 break;
314
315 case 10U ... 99U:
316 i = 1;
317 break;
318
319 case 100U ... 999U:
320 i = 2;
321 break;
322
323 case 1000U ... 9999U:
324 i = 3;
325 break;
326
327 case 10000U ... 99999U:
328 i = 4;
329 break;
330
331 case 100000U ... 999999U:
332 i = 5;
333 break;
334
335 case 1000000U ... 9999999U:
336 i = 6;
337 break;
338
339 case 10000000U ... 99999999U:
340 i = 7;
341 break;
342
343 case 100000000U ... 999999999U:
344 i = 8;
345 break;
346
347 case 1000000000U ... 4294967295U:
348 i = 9;
349 break;
350 }
351 if (i + 2 > size) // (i + 1) + '\0'
352 return NULL; // too long
353 if (i < size)
354 i = size - 2; // padding - '\0'
355
356 ret = dst + i + 1;
357 *ret = '\0';
358 for (; i >= 0; i--) {
359 dst[i] = n % 10U + '0';
360 n /= 10U;
361 }
362 return ret;
363}
364
365/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200366 * copies at most <size-1> chars from <src> to <dst>. Last char is always
367 * set to 0, unless <size> is 0. The number of chars copied is returned
368 * (excluding the terminating zero).
369 * This code has been optimized for size and speed : on x86, it's 45 bytes
370 * long, uses only registers, and consumes only 4 cycles per char.
371 */
372int strlcpy2(char *dst, const char *src, int size)
373{
374 char *orig = dst;
375 if (size) {
376 while (--size && (*dst = *src)) {
377 src++; dst++;
378 }
379 *dst = 0;
380 }
381 return dst - orig;
382}
383
384/*
Willy Tarreau72d759c2007-10-25 12:14:10 +0200385 * This function simply returns a locally allocated string containing
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386 * the ascii representation for number 'n' in decimal.
387 */
Emeric Brun3a7fce52010-01-04 14:54:38 +0100388char *ultoa_r(unsigned long n, char *buffer, int size)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200389{
390 char *pos;
391
Willy Tarreau72d759c2007-10-25 12:14:10 +0200392 pos = buffer + size - 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200393 *pos-- = '\0';
394
395 do {
396 *pos-- = '0' + n % 10;
397 n /= 10;
Willy Tarreau72d759c2007-10-25 12:14:10 +0200398 } while (n && pos >= buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200399 return pos + 1;
400}
401
Willy Tarreau91092e52007-10-25 16:58:42 +0200402/*
Willy Tarreaue7239b52009-03-29 13:41:58 +0200403 * This function simply returns a locally allocated string containing
404 * the ascii representation for number 'n' in decimal, formatted for
405 * HTML output with tags to create visual grouping by 3 digits. The
406 * output needs to support at least 171 characters.
407 */
408const char *ulltoh_r(unsigned long long n, char *buffer, int size)
409{
410 char *start;
411 int digit = 0;
412
413 start = buffer + size;
414 *--start = '\0';
415
416 do {
417 if (digit == 3 && start >= buffer + 7)
418 memcpy(start -= 7, "</span>", 7);
419
420 if (start >= buffer + 1) {
421 *--start = '0' + n % 10;
422 n /= 10;
423 }
424
425 if (digit == 3 && start >= buffer + 18)
426 memcpy(start -= 18, "<span class=\"rls\">", 18);
427
428 if (digit++ == 3)
429 digit = 1;
430 } while (n && start > buffer);
431 return start;
432}
433
434/*
Willy Tarreau91092e52007-10-25 16:58:42 +0200435 * This function simply returns a locally allocated string containing the ascii
436 * representation for number 'n' in decimal, unless n is 0 in which case it
437 * returns the alternate string (or an empty string if the alternate string is
438 * NULL). It use is intended for limits reported in reports, where it's
439 * desirable not to display anything if there is no limit. Warning! it shares
440 * the same vector as ultoa_r().
441 */
442const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
443{
444 return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
445}
446
Robert Tsai81ae1952007-12-05 10:47:29 +0100447/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200448 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
449 *
450 * It looks like this one would be a good candidate for inlining, but this is
451 * not interesting because it around 35 bytes long and often called multiple
452 * times within the same function.
453 */
454int ishex(char s)
455{
456 s -= '0';
457 if ((unsigned char)s <= 9)
458 return 1;
459 s -= 'A' - '0';
460 if ((unsigned char)s <= 5)
461 return 1;
462 s -= 'a' - 'A';
463 if ((unsigned char)s <= 5)
464 return 1;
465 return 0;
466}
467
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100468/*
469 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
470 * invalid character is found, a pointer to it is returned. If everything is
471 * fine, NULL is returned.
472 */
473const char *invalid_char(const char *name)
474{
475 if (!*name)
476 return name;
477
478 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100479 if (!isalnum((int)(unsigned char)*name) && *name != '.' && *name != ':' &&
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100480 *name != '_' && *name != '-')
481 return name;
482 name++;
483 }
484 return NULL;
485}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200486
487/*
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200488 * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-].
489 * If an invalid character is found, a pointer to it is returned.
490 * If everything is fine, NULL is returned.
491 */
492const char *invalid_domainchar(const char *name) {
493
494 if (!*name)
495 return name;
496
497 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100498 if (!isalnum((int)(unsigned char)*name) && *name != '.' &&
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200499 *name != '_' && *name != '-')
500 return name;
501
502 name++;
503 }
504
505 return NULL;
506}
507
508/*
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100509 * converts <str> to a struct sockaddr_storage* provided by the caller. The
Willy Tarreau24709282013-03-10 21:32:12 +0100510 * caller must have zeroed <sa> first, and may have set sa->ss_family to force
511 * parse a specific address format. If the ss_family is 0 or AF_UNSPEC, then
512 * the function tries to guess the address family from the syntax. If the
513 * family is forced and the format doesn't match, an error is returned. The
Willy Tarreaufab5a432011-03-04 15:31:53 +0100514 * string is assumed to contain only an address, no port. The address can be a
515 * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to
516 * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved.
517 * The return address will only have the address family and the address set,
518 * all other fields remain zero. The string is not supposed to be modified.
519 * The IPv6 '::' address is IN6ADDR_ANY.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200520 */
Willy Tarreau24709282013-03-10 21:32:12 +0100521static struct sockaddr_storage *str2ip(const char *str, struct sockaddr_storage *sa)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200522{
Willy Tarreaufab5a432011-03-04 15:31:53 +0100523 struct hostent *he;
524
Willy Tarreaufab5a432011-03-04 15:31:53 +0100525 /* Any IPv6 address */
526 if (str[0] == ':' && str[1] == ':' && !str[2]) {
Willy Tarreau24709282013-03-10 21:32:12 +0100527 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
528 sa->ss_family = AF_INET6;
529 else if (sa->ss_family != AF_INET6)
530 goto fail;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100531 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100532 }
533
Willy Tarreau24709282013-03-10 21:32:12 +0100534 /* Any address for the family, defaults to IPv4 */
Willy Tarreaufab5a432011-03-04 15:31:53 +0100535 if (!str[0] || (str[0] == '*' && !str[1])) {
Willy Tarreau24709282013-03-10 21:32:12 +0100536 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
537 sa->ss_family = AF_INET;
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100538 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100539 }
540
541 /* check for IPv6 first */
Willy Tarreau24709282013-03-10 21:32:12 +0100542 if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET6) &&
543 inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)sa)->sin6_addr)) {
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100544 sa->ss_family = AF_INET6;
545 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100546 }
547
548 /* then check for IPv4 */
Willy Tarreau24709282013-03-10 21:32:12 +0100549 if ((!sa->ss_family || sa->ss_family == AF_UNSPEC || sa->ss_family == AF_INET) &&
550 inet_pton(AF_INET, str, &((struct sockaddr_in *)sa)->sin_addr)) {
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100551 sa->ss_family = AF_INET;
552 return sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100553 }
554
David du Colombierd5f43282011-03-17 10:40:16 +0100555#ifdef USE_GETADDRINFO
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200556 if (global.tune.options & GTUNE_USE_GAI) {
David du Colombierd5f43282011-03-17 10:40:16 +0100557 struct addrinfo hints, *result;
558
559 memset(&result, 0, sizeof(result));
560 memset(&hints, 0, sizeof(hints));
Willy Tarreau24709282013-03-10 21:32:12 +0100561 hints.ai_family = sa->ss_family ? sa->ss_family : AF_UNSPEC;
David du Colombierd5f43282011-03-17 10:40:16 +0100562 hints.ai_socktype = SOCK_DGRAM;
563 hints.ai_flags = AI_PASSIVE;
564 hints.ai_protocol = 0;
565
566 if (getaddrinfo(str, NULL, &hints, &result) == 0) {
Willy Tarreau24709282013-03-10 21:32:12 +0100567 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
568 sa->ss_family = result->ai_family;
569 else if (sa->ss_family != result->ai_family)
570 goto fail;
571
David du Colombierd5f43282011-03-17 10:40:16 +0100572 switch (result->ai_family) {
573 case AF_INET:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100574 memcpy((struct sockaddr_in *)sa, result->ai_addr, result->ai_addrlen);
575 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100576 case AF_INET6:
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100577 memcpy((struct sockaddr_in6 *)sa, result->ai_addr, result->ai_addrlen);
578 return sa;
David du Colombierd5f43282011-03-17 10:40:16 +0100579 }
580 }
581
Sean Carey58ea0392013-02-15 23:39:18 +0100582 if (result)
583 freeaddrinfo(result);
Willy Tarreaufab5a432011-03-04 15:31:53 +0100584 }
David du Colombierd5f43282011-03-17 10:40:16 +0100585#endif
Nenad Merdanovic88afe032014-04-14 15:56:58 +0200586 /* try to resolve an IPv4/IPv6 hostname */
587 he = gethostbyname(str);
588 if (he) {
589 if (!sa->ss_family || sa->ss_family == AF_UNSPEC)
590 sa->ss_family = he->h_addrtype;
591 else if (sa->ss_family != he->h_addrtype)
592 goto fail;
593
594 switch (sa->ss_family) {
595 case AF_INET:
596 ((struct sockaddr_in *)sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
597 return sa;
598 case AF_INET6:
599 ((struct sockaddr_in6 *)sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
600 return sa;
601 }
602 }
603
David du Colombierd5f43282011-03-17 10:40:16 +0100604 /* unsupported address family */
Willy Tarreau24709282013-03-10 21:32:12 +0100605 fail:
Willy Tarreaufab5a432011-03-04 15:31:53 +0100606 return NULL;
607}
608
609/*
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100610 * Converts <str> to a locally allocated struct sockaddr_storage *, and a port
611 * range or offset consisting in two integers that the caller will have to
612 * check to find the relevant input format. The following format are supported :
613 *
614 * String format | address | port | low | high
615 * addr | <addr> | 0 | 0 | 0
616 * addr: | <addr> | 0 | 0 | 0
617 * addr:port | <addr> | <port> | <port> | <port>
618 * addr:pl-ph | <addr> | <pl> | <pl> | <ph>
619 * addr:+port | <addr> | <port> | 0 | <port>
620 * addr:-port | <addr> |-<port> | <port> | 0
621 *
622 * The detection of a port range or increment by the caller is made by
623 * comparing <low> and <high>. If both are equal, then port 0 means no port
624 * was specified. The caller may pass NULL for <low> and <high> if it is not
625 * interested in retrieving port ranges.
626 *
627 * Note that <addr> above may also be :
628 * - empty ("") => family will be AF_INET and address will be INADDR_ANY
629 * - "*" => family will be AF_INET and address will be INADDR_ANY
630 * - "::" => family will be AF_INET6 and address will be IN6ADDR_ANY
631 * - a host name => family and address will depend on host name resolving.
632 *
Willy Tarreau24709282013-03-10 21:32:12 +0100633 * A prefix may be passed in before the address above to force the family :
634 * - "ipv4@" => force address to resolve as IPv4 and fail if not possible.
635 * - "ipv6@" => force address to resolve as IPv6 and fail if not possible.
636 * - "unix@" => force address to be a path to a UNIX socket even if the
637 * path does not start with a '/'
Willy Tarreauccfccef2014-05-10 01:49:15 +0200638 * - 'abns@' -> force address to belong to the abstract namespace (Linux
639 * only). These sockets are just like Unix sockets but without
640 * the need for an underlying file system. The address is a
641 * string. Technically it's like a Unix socket with a zero in
642 * the first byte of the address.
Willy Tarreau40aa0702013-03-10 23:51:38 +0100643 * - "fd@" => an integer must follow, and is a file descriptor number.
Willy Tarreau24709282013-03-10 21:32:12 +0100644 *
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100645 * Also note that in order to avoid any ambiguity with IPv6 addresses, the ':'
646 * is mandatory after the IP address even when no port is specified. NULL is
647 * returned if the address cannot be parsed. The <low> and <high> ports are
Willy Tarreau24709282013-03-10 21:32:12 +0100648 * always initialized if non-null, even for non-IP families.
Willy Tarreaud393a622013-03-04 18:22:00 +0100649 *
650 * If <pfx> is non-null, it is used as a string prefix before any path-based
651 * address (typically the path to a unix socket).
Willy Tarreau40aa0702013-03-10 23:51:38 +0100652 *
653 * When a file descriptor is passed, its value is put into the s_addr part of
654 * the address when cast to sockaddr_in and the address family is AF_UNSPEC.
Willy Tarreaufab5a432011-03-04 15:31:53 +0100655 */
Willy Tarreaud393a622013-03-04 18:22:00 +0100656struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high, char **err, const char *pfx)
Willy Tarreaufab5a432011-03-04 15:31:53 +0100657{
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100658 static struct sockaddr_storage ss;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100659 struct sockaddr_storage *ret = NULL;
Willy Tarreau24709282013-03-10 21:32:12 +0100660 char *back, *str2;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100661 char *port1, *port2;
662 int portl, porth, porta;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200663 int abstract = 0;
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100664
665 portl = porth = porta = 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200666
Willy Tarreaudad36a32013-03-11 01:20:04 +0100667 str2 = back = env_expand(strdup(str));
Willy Tarreaudf350f12013-03-01 20:22:54 +0100668 if (str2 == NULL) {
669 memprintf(err, "out of memory in '%s'\n", __FUNCTION__);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100670 goto out;
Willy Tarreaudf350f12013-03-01 20:22:54 +0100671 }
Willy Tarreaubaaee002006-06-26 02:48:02 +0200672
Willy Tarreau24709282013-03-10 21:32:12 +0100673 memset(&ss, 0, sizeof(ss));
674
675 if (strncmp(str2, "unix@", 5) == 0) {
676 str2 += 5;
Willy Tarreauccfccef2014-05-10 01:49:15 +0200677 abstract = 0;
Willy Tarreau24709282013-03-10 21:32:12 +0100678 ss.ss_family = AF_UNIX;
679 }
Willy Tarreauccfccef2014-05-10 01:49:15 +0200680 else if (strncmp(str2, "abns@", 5) == 0) {
681 str2 += 5;
682 abstract = 1;
683 ss.ss_family = AF_UNIX;
684 }
Willy Tarreau24709282013-03-10 21:32:12 +0100685 else if (strncmp(str2, "ipv4@", 5) == 0) {
686 str2 += 5;
687 ss.ss_family = AF_INET;
688 }
689 else if (strncmp(str2, "ipv6@", 5) == 0) {
690 str2 += 5;
691 ss.ss_family = AF_INET6;
692 }
693 else if (*str2 == '/') {
694 ss.ss_family = AF_UNIX;
695 }
696 else
697 ss.ss_family = AF_UNSPEC;
698
Willy Tarreau40aa0702013-03-10 23:51:38 +0100699 if (ss.ss_family == AF_UNSPEC && strncmp(str2, "fd@", 3) == 0) {
700 char *endptr;
701
702 str2 += 3;
703 ((struct sockaddr_in *)&ss)->sin_addr.s_addr = strtol(str2, &endptr, 10);
704
705 if (!*str2 || *endptr) {
Willy Tarreaudad36a32013-03-11 01:20:04 +0100706 memprintf(err, "file descriptor '%s' is not a valid integer in '%s'\n", str2, str);
Willy Tarreau40aa0702013-03-10 23:51:38 +0100707 goto out;
708 }
709
710 /* we return AF_UNSPEC if we use a file descriptor number */
711 ss.ss_family = AF_UNSPEC;
712 }
713 else if (ss.ss_family == AF_UNIX) {
Willy Tarreau15586382013-03-04 19:48:14 +0100714 int prefix_path_len;
715 int max_path_len;
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200716 int adr_len;
Willy Tarreau15586382013-03-04 19:48:14 +0100717
718 /* complete unix socket path name during startup or soft-restart is
719 * <unix_bind_prefix><path>.<pid>.<bak|tmp>
720 */
Willy Tarreauccfccef2014-05-10 01:49:15 +0200721 prefix_path_len = (pfx && !abstract) ? strlen(pfx) : 0;
Willy Tarreau15586382013-03-04 19:48:14 +0100722 max_path_len = (sizeof(((struct sockaddr_un *)&ss)->sun_path) - 1) -
723 (prefix_path_len ? prefix_path_len + 1 + 5 + 1 + 3 : 0);
724
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200725 adr_len = strlen(str2);
726 if (adr_len > max_path_len) {
Willy Tarreau15586382013-03-04 19:48:14 +0100727 memprintf(err, "socket path '%s' too long (max %d)\n", str, max_path_len);
728 goto out;
729 }
730
Willy Tarreauccfccef2014-05-10 01:49:15 +0200731 /* when abstract==1, we skip the first zero and copy all bytes except the trailing zero */
732 memset(((struct sockaddr_un *)&ss)->sun_path, 0, sizeof(((struct sockaddr_un *)&ss)->sun_path));
Willy Tarreau94ef3f32014-04-14 14:49:00 +0200733 if (prefix_path_len)
Willy Tarreau15586382013-03-04 19:48:14 +0100734 memcpy(((struct sockaddr_un *)&ss)->sun_path, pfx, prefix_path_len);
Willy Tarreauccfccef2014-05-10 01:49:15 +0200735 memcpy(((struct sockaddr_un *)&ss)->sun_path + prefix_path_len + abstract, str2, adr_len + 1 - abstract);
Willy Tarreau15586382013-03-04 19:48:14 +0100736 }
Willy Tarreau24709282013-03-10 21:32:12 +0100737 else { /* IPv4 and IPv6 */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100738 port1 = strrchr(str2, ':');
739 if (port1)
740 *port1++ = '\0';
741 else
742 port1 = "";
Willy Tarreaubaaee002006-06-26 02:48:02 +0200743
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100744 if (str2ip(str2, &ss) == NULL) {
745 memprintf(err, "invalid address: '%s' in '%s'\n", str2, str);
746 goto out;
747 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100748
Willy Tarreaua39d1992013-04-01 20:37:42 +0200749 if (isdigit((int)(unsigned char)*port1)) { /* single port or range */
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100750 port2 = strchr(port1, '-');
751 if (port2)
752 *port2++ = '\0';
753 else
754 port2 = port1;
755 portl = atoi(port1);
756 porth = atoi(port2);
757 porta = portl;
758 }
759 else if (*port1 == '-') { /* negative offset */
760 portl = atoi(port1 + 1);
761 porta = -portl;
762 }
763 else if (*port1 == '+') { /* positive offset */
764 porth = atoi(port1 + 1);
765 porta = porth;
766 }
767 else if (*port1) { /* other any unexpected char */
Willy Tarreaudad36a32013-03-11 01:20:04 +0100768 memprintf(err, "invalid character '%c' in port number '%s' in '%s'\n", *port1, port1, str);
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100769 goto out;
770 }
771 set_host_port(&ss, porta);
Willy Tarreaue4c58c82013-03-06 15:28:17 +0100772 }
Willy Tarreaufab5a432011-03-04 15:31:53 +0100773
Willy Tarreauc120c8d2013-03-10 19:27:44 +0100774 ret = &ss;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100775 out:
Willy Tarreaud4448bc2013-02-20 15:55:15 +0100776 if (low)
777 *low = portl;
778 if (high)
779 *high = porth;
Willy Tarreau24709282013-03-10 21:32:12 +0100780 free(back);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100781 return ret;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200782}
783
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100784/* converts <str> to a struct in_addr containing a network mask. It can be
785 * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1
786 * if the conversion succeeds otherwise non-zero.
787 */
788int str2mask(const char *str, struct in_addr *mask)
789{
790 if (strchr(str, '.') != NULL) { /* dotted notation */
791 if (!inet_pton(AF_INET, str, mask))
792 return 0;
793 }
794 else { /* mask length */
795 char *err;
796 unsigned long len = strtol(str, &err, 10);
797
798 if (!*str || (err && *err) || (unsigned)len > 32)
799 return 0;
800 if (len)
801 mask->s_addr = htonl(~0UL << (32 - len));
802 else
803 mask->s_addr = 0;
804 }
805 return 1;
806}
807
Thierry FOURNIERb0504632013-12-14 15:39:02 +0100808/* convert <cidr> to struct in_addr <mask>. It returns 1 if the conversion
809 * succeeds otherwise zero.
810 */
811int cidr2dotted(int cidr, struct in_addr *mask) {
812
813 if (cidr < 0 || cidr > 32)
814 return 0;
815
816 mask->s_addr = cidr ? htonl(~0UL << (32 - cidr)) : 0;
817 return 1;
818}
819
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200820/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200821 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200822 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
823 * is optionnal and either in the dotted or CIDR notation.
824 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
825 */
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100826int str2net(const char *str, int resolve, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200827{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200828 __label__ out_free, out_err;
829 char *c, *s;
830 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200831
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200832 s = strdup(str);
833 if (!s)
834 return 0;
835
Willy Tarreaubaaee002006-06-26 02:48:02 +0200836 memset(mask, 0, sizeof(*mask));
837 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200838
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200839 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200840 *c++ = '\0';
841 /* c points to the mask */
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100842 if (!str2mask(c, mask))
843 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200844 }
845 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100846 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200847 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200848 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200849 struct hostent *he;
850
Thierry FOURNIERfc7ac7b2014-02-11 15:23:04 +0100851 if (!resolve)
852 goto out_err;
853
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200854 if ((he = gethostbyname(s)) == NULL) {
855 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200856 }
857 else
858 *addr = *(struct in_addr *) *(he->h_addr_list);
859 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200860
861 ret_val = 1;
862 out_free:
863 free(s);
864 return ret_val;
865 out_err:
866 ret_val = 0;
867 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200868}
869
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100870
871/*
Willy Tarreau6d20e282012-04-27 22:49:47 +0200872 * converts <str> to two struct in6_addr* which must be pre-allocated.
873 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
874 * is an optionnal number of bits (128 being the default).
875 * Returns 1 if OK, 0 if error.
876 */
877int str62net(const char *str, struct in6_addr *addr, unsigned char *mask)
878{
879 char *c, *s;
880 int ret_val = 0;
881 char *err;
882 unsigned long len = 128;
883
884 s = strdup(str);
885 if (!s)
886 return 0;
887
888 memset(mask, 0, sizeof(*mask));
889 memset(addr, 0, sizeof(*addr));
890
891 if ((c = strrchr(s, '/')) != NULL) {
892 *c++ = '\0'; /* c points to the mask */
893 if (!*c)
894 goto out_free;
895
896 len = strtoul(c, &err, 10);
897 if ((err && *err) || (unsigned)len > 128)
898 goto out_free;
899 }
900 *mask = len; /* OK we have a valid mask in <len> */
901
902 if (!inet_pton(AF_INET6, s, addr))
903 goto out_free;
904
905 ret_val = 1;
906 out_free:
907 free(s);
908 return ret_val;
909}
910
911
912/*
David du Colombier6f5ccb12011-03-10 22:26:24 +0100913 * Parse IPv4 address found in url.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100914 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100915int url2ipv4(const char *addr, struct in_addr *dst)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100916{
917 int saw_digit, octets, ch;
918 u_char tmp[4], *tp;
919 const char *cp = addr;
920
921 saw_digit = 0;
922 octets = 0;
923 *(tp = tmp) = 0;
924
925 while (*addr) {
926 unsigned char digit = (ch = *addr++) - '0';
927 if (digit > 9 && ch != '.')
928 break;
929 if (digit <= 9) {
930 u_int new = *tp * 10 + digit;
931 if (new > 255)
932 return 0;
933 *tp = new;
934 if (!saw_digit) {
935 if (++octets > 4)
936 return 0;
937 saw_digit = 1;
938 }
939 } else if (ch == '.' && saw_digit) {
940 if (octets == 4)
941 return 0;
942 *++tp = 0;
943 saw_digit = 0;
944 } else
945 return 0;
946 }
947
948 if (octets < 4)
949 return 0;
950
951 memcpy(&dst->s_addr, tmp, 4);
952 return addr-cp-1;
953}
954
955/*
Thierry FOURNIER9f95e402014-03-21 14:51:46 +0100956 * Resolve destination server from URL. Convert <str> to a sockaddr_storage.
957 * <out> contain the code of the dectected scheme, the start and length of
958 * the hostname. Actually only http and https are supported. <out> can be NULL.
959 * This function returns the consumed length. It is useful if you parse complete
960 * url like http://host:port/path, because the consumed length corresponds to
961 * the first character of the path. If the conversion fails, it returns -1.
962 *
963 * This function tries to resolve the DNS name if haproxy is in starting mode.
964 * So, this function may be used during the configuration parsing.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100965 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +0100966int url2sa(const char *url, int ulen, struct sockaddr_storage *addr, struct split_url *out)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100967{
968 const char *curr = url, *cp = url;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +0100969 const char *end;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100970 int ret, url_code = 0;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +0100971 unsigned long long int http_code = 0;
972 int default_port;
973 struct hostent *he;
974 char *p;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100975
976 /* Firstly, try to find :// pattern */
977 while (curr < url+ulen && url_code != 0x3a2f2f) {
978 url_code = ((url_code & 0xffff) << 8);
979 url_code += (unsigned char)*curr++;
980 }
981
982 /* Secondly, if :// pattern is found, verify parsed stuff
983 * before pattern is matching our http pattern.
984 * If so parse ip address and port in uri.
985 *
986 * WARNING: Current code doesn't support dynamic async dns resolver.
987 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +0100988 if (url_code != 0x3a2f2f)
989 return -1;
990
991 /* Copy scheme, and utrn to lower case. */
992 while (cp < curr - 3)
993 http_code = (http_code << 8) + *cp++;
994 http_code |= 0x2020202020202020ULL; /* Turn everything to lower case */
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100995
Thierry FOURNIER9f95e402014-03-21 14:51:46 +0100996 /* HTTP or HTTPS url matching */
997 if (http_code == 0x2020202068747470ULL) {
998 default_port = 80;
999 if (out)
1000 out->scheme = SCH_HTTP;
1001 }
1002 else if (http_code == 0x2020206874747073ULL) {
1003 default_port = 443;
1004 if (out)
1005 out->scheme = SCH_HTTPS;
1006 }
1007 else
1008 return -1;
1009
1010 /* If the next char is '[', the host address is IPv6. */
1011 if (*curr == '[') {
1012 curr++;
1013
1014 /* Check trash size */
1015 if (trash.size < ulen)
1016 return -1;
1017
1018 /* Look for ']' and copy the address in a trash buffer. */
1019 p = trash.str;
1020 for (end = curr;
1021 end < url + ulen && *end != ']';
1022 end++, p++)
1023 *p = *end;
1024 if (*end != ']')
1025 return -1;
1026 *p = '\0';
1027
1028 /* Update out. */
1029 if (out) {
1030 out->host = curr;
1031 out->host_len = end - curr;
1032 }
1033
1034 /* Try IPv6 decoding. */
1035 if (!inet_pton(AF_INET6, trash.str, &((struct sockaddr_in6 *)addr)->sin6_addr))
1036 return -1;
1037 end++;
1038
1039 /* Decode port. */
1040 if (*end == ':') {
1041 end++;
1042 default_port = read_uint(&end, url + ulen);
1043 }
1044 ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port);
1045 ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
1046 return end - url;
1047 }
1048 else {
1049 /* We are looking for IP address. If you want to parse and
1050 * resolve hostname found in url, you can use str2sa_range(), but
1051 * be warned this can slow down global daemon performances
1052 * while handling lagging dns responses.
1053 */
1054 ret = url2ipv4(curr, &((struct sockaddr_in *)addr)->sin_addr);
1055 if (ret) {
1056 /* Update out. */
1057 if (out) {
1058 out->host = curr;
1059 out->host_len = ret;
1060 }
1061
1062 curr += ret;
1063
1064 /* Decode port. */
1065 if (*curr == ':') {
1066 curr++;
1067 default_port = read_uint(&curr, url + ulen);
1068 }
1069 ((struct sockaddr_in *)addr)->sin_port = htons(default_port);
1070
1071 /* Set family. */
1072 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
1073 return curr - url;
1074 }
1075 else if (global.mode & MODE_STARTING) {
1076 /* The IPv4 and IPv6 decoding fails, maybe the url contain name. Try to execute
1077 * synchronous DNS request only if HAProxy is in the start state.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001078 */
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001079
1080 /* look for : or / or end */
1081 for (end = curr;
1082 end < url + ulen && *end != '/' && *end != ':';
1083 end++);
1084 memcpy(trash.str, curr, end - curr);
1085 trash.str[end - curr] = '\0';
1086
1087 /* try to resolve an IPv4/IPv6 hostname */
1088 he = gethostbyname(trash.str);
1089 if (!he)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001090 return -1;
Thierry FOURNIER9f95e402014-03-21 14:51:46 +01001091
1092 /* Update out. */
1093 if (out) {
1094 out->host = curr;
1095 out->host_len = end - curr;
1096 }
1097
1098 /* Decode port. */
1099 if (*end == ':') {
1100 end++;
1101 default_port = read_uint(&end, url + ulen);
1102 }
1103
1104 /* Copy IP address, set port and family. */
1105 switch (he->h_addrtype) {
1106 case AF_INET:
1107 ((struct sockaddr_in *)addr)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
1108 ((struct sockaddr_in *)addr)->sin_port = htons(default_port);
1109 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
1110 return end - url;
1111
1112 case AF_INET6:
1113 ((struct sockaddr_in6 *)addr)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
1114 ((struct sockaddr_in6 *)addr)->sin6_port = htons(default_port);
1115 ((struct sockaddr_in6 *)addr)->sin6_family = AF_INET6;
1116 return end - url;
1117 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001118 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001119 }
Alexandre Cassen5eb1a902007-11-29 15:43:32 +01001120 return -1;
1121}
1122
Willy Tarreau631f01c2011-09-05 00:36:48 +02001123/* Tries to convert a sockaddr_storage address to text form. Upon success, the
1124 * address family is returned so that it's easy for the caller to adapt to the
1125 * output format. Zero is returned if the address family is not supported. -1
1126 * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
1127 * supported.
1128 */
1129int addr_to_str(struct sockaddr_storage *addr, char *str, int size)
1130{
1131
1132 void *ptr;
1133
1134 if (size < 5)
1135 return 0;
1136 *str = '\0';
1137
1138 switch (addr->ss_family) {
1139 case AF_INET:
1140 ptr = &((struct sockaddr_in *)addr)->sin_addr;
1141 break;
1142 case AF_INET6:
1143 ptr = &((struct sockaddr_in6 *)addr)->sin6_addr;
1144 break;
1145 case AF_UNIX:
1146 memcpy(str, "unix", 5);
1147 return addr->ss_family;
1148 default:
1149 return 0;
1150 }
1151
1152 if (inet_ntop(addr->ss_family, ptr, str, size))
1153 return addr->ss_family;
1154
1155 /* failed */
1156 return -1;
1157}
1158
Willy Tarreaubaaee002006-06-26 02:48:02 +02001159/* will try to encode the string <string> replacing all characters tagged in
1160 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
1161 * prefixed by <escape>, and will store the result between <start> (included)
1162 * and <stop> (excluded), and will always terminate the string with a '\0'
1163 * before <stop>. The position of the '\0' is returned if the conversion
1164 * completes. If bytes are missing between <start> and <stop>, then the
1165 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
1166 * cannot even be stored so we return <start> without writing the 0.
1167 * The input string must also be zero-terminated.
1168 */
1169const char hextab[16] = "0123456789ABCDEF";
1170char *encode_string(char *start, char *stop,
1171 const char escape, const fd_set *map,
1172 const char *string)
1173{
1174 if (start < stop) {
1175 stop--; /* reserve one byte for the final '\0' */
1176 while (start < stop && *string != '\0') {
1177 if (!FD_ISSET((unsigned char)(*string), map))
1178 *start++ = *string;
1179 else {
1180 if (start + 3 >= stop)
1181 break;
1182 *start++ = escape;
1183 *start++ = hextab[(*string >> 4) & 15];
1184 *start++ = hextab[*string & 15];
1185 }
1186 string++;
1187 }
1188 *start = '\0';
1189 }
1190 return start;
1191}
1192
Thierry FOURNIERe059ec92014-03-17 12:01:13 +01001193/*
1194 * Same behavior as encode_string() above, except that it encodes chunk
1195 * <chunk> instead of a string.
1196 */
1197char *encode_chunk(char *start, char *stop,
1198 const char escape, const fd_set *map,
1199 const struct chunk *chunk)
1200{
1201 char *str = chunk->str;
1202 char *end = chunk->str + chunk->len;
1203
1204 if (start < stop) {
1205 stop--; /* reserve one byte for the final '\0' */
1206 while (start < stop && str < end) {
1207 if (!FD_ISSET((unsigned char)(*str), map))
1208 *start++ = *str;
1209 else {
1210 if (start + 3 >= stop)
1211 break;
1212 *start++ = escape;
1213 *start++ = hextab[(*str >> 4) & 15];
1214 *start++ = hextab[*str & 15];
1215 }
1216 str++;
1217 }
1218 *start = '\0';
1219 }
1220 return start;
1221}
1222
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001223/* Decode an URL-encoded string in-place. The resulting string might
1224 * be shorter. If some forbidden characters are found, the conversion is
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001225 * aborted, the string is truncated before the issue and a negative value is
1226 * returned, otherwise the operation returns the length of the decoded string.
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001227 */
1228int url_decode(char *string)
1229{
1230 char *in, *out;
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001231 int ret = -1;
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001232
1233 in = string;
1234 out = string;
1235 while (*in) {
1236 switch (*in) {
1237 case '+' :
1238 *out++ = ' ';
1239 break;
1240 case '%' :
1241 if (!ishex(in[1]) || !ishex(in[2]))
1242 goto end;
1243 *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]);
1244 in += 2;
1245 break;
1246 default:
1247 *out++ = *in;
1248 break;
1249 }
1250 in++;
1251 }
Thierry FOURNIER5068d962013-10-04 16:27:27 +02001252 ret = out - string; /* success */
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +02001253 end:
1254 *out = 0;
1255 return ret;
1256}
Willy Tarreaubaaee002006-06-26 02:48:02 +02001257
Willy Tarreau6911fa42007-03-04 18:06:08 +01001258unsigned int str2ui(const char *s)
1259{
1260 return __str2ui(s);
1261}
1262
1263unsigned int str2uic(const char *s)
1264{
1265 return __str2uic(s);
1266}
1267
1268unsigned int strl2ui(const char *s, int len)
1269{
1270 return __strl2ui(s, len);
1271}
1272
1273unsigned int strl2uic(const char *s, int len)
1274{
1275 return __strl2uic(s, len);
1276}
1277
Willy Tarreau4ec83cd2010-10-15 23:19:55 +02001278unsigned int read_uint(const char **s, const char *end)
1279{
1280 return __read_uint(s, end);
1281}
1282
Willy Tarreau6911fa42007-03-04 18:06:08 +01001283/* This one is 7 times faster than strtol() on athlon with checks.
1284 * It returns the value of the number composed of all valid digits read,
1285 * and can process negative numbers too.
1286 */
1287int strl2ic(const char *s, int len)
1288{
1289 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001290 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001291
1292 if (len > 0) {
1293 if (*s != '-') {
1294 /* positive number */
1295 while (len-- > 0) {
1296 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001297 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001298 if (j > 9)
1299 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001300 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001301 }
1302 } else {
1303 /* negative number */
1304 s++;
1305 while (--len > 0) {
1306 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001307 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001308 if (j > 9)
1309 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +02001310 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +01001311 }
1312 }
1313 }
1314 return i;
1315}
1316
1317
1318/* This function reads exactly <len> chars from <s> and converts them to a
1319 * signed integer which it stores into <ret>. It accurately detects any error
1320 * (truncated string, invalid chars, overflows). It is meant to be used in
1321 * applications designed for hostile environments. It returns zero when the
1322 * number has successfully been converted, non-zero otherwise. When an error
1323 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
1324 * faster than strtol().
1325 */
1326int strl2irc(const char *s, int len, int *ret)
1327{
1328 int i = 0;
1329 int j;
1330
1331 if (!len)
1332 return 1;
1333
1334 if (*s != '-') {
1335 /* positive number */
1336 while (len-- > 0) {
1337 j = (*s++) - '0';
1338 if (j > 9) return 1; /* invalid char */
1339 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
1340 i = i * 10;
1341 if (i + j < i) return 1; /* check for addition overflow */
1342 i = i + j;
1343 }
1344 } else {
1345 /* negative number */
1346 s++;
1347 while (--len > 0) {
1348 j = (*s++) - '0';
1349 if (j > 9) return 1; /* invalid char */
1350 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
1351 i = i * 10;
1352 if (i - j > i) return 1; /* check for subtract overflow */
1353 i = i - j;
1354 }
1355 }
1356 *ret = i;
1357 return 0;
1358}
1359
1360
1361/* This function reads exactly <len> chars from <s> and converts them to a
1362 * signed integer which it stores into <ret>. It accurately detects any error
1363 * (truncated string, invalid chars, overflows). It is meant to be used in
1364 * applications designed for hostile environments. It returns zero when the
1365 * number has successfully been converted, non-zero otherwise. When an error
1366 * is returned, the <ret> value is left untouched. It is about 3 times slower
1367 * than str2irc().
1368 */
Willy Tarreau6911fa42007-03-04 18:06:08 +01001369
1370int strl2llrc(const char *s, int len, long long *ret)
1371{
1372 long long i = 0;
1373 int j;
1374
1375 if (!len)
1376 return 1;
1377
1378 if (*s != '-') {
1379 /* positive number */
1380 while (len-- > 0) {
1381 j = (*s++) - '0';
1382 if (j > 9) return 1; /* invalid char */
1383 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
1384 i = i * 10LL;
1385 if (i + j < i) return 1; /* check for addition overflow */
1386 i = i + j;
1387 }
1388 } else {
1389 /* negative number */
1390 s++;
1391 while (--len > 0) {
1392 j = (*s++) - '0';
1393 if (j > 9) return 1; /* invalid char */
1394 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
1395 i = i * 10LL;
1396 if (i - j > i) return 1; /* check for subtract overflow */
1397 i = i - j;
1398 }
1399 }
1400 *ret = i;
1401 return 0;
1402}
1403
Thierry FOURNIER511e9472014-01-23 17:40:34 +01001404/* This function is used with pat_parse_dotted_ver(). It converts a string
1405 * composed by two number separated by a dot. Each part must contain in 16 bits
1406 * because internally they will be represented as a 32-bit quantity stored in
1407 * a 64-bit integer. It returns zero when the number has successfully been
1408 * converted, non-zero otherwise. When an error is returned, the <ret> value
1409 * is left untouched.
1410 *
1411 * "1.3" -> 0x0000000000010003
1412 * "65535.65535" -> 0x00000000ffffffff
1413 */
1414int strl2llrc_dotted(const char *text, int len, long long *ret)
1415{
1416 const char *end = &text[len];
1417 const char *p;
1418 long long major, minor;
1419
1420 /* Look for dot. */
1421 for (p = text; p < end; p++)
1422 if (*p == '.')
1423 break;
1424
1425 /* Convert major. */
1426 if (strl2llrc(text, p - text, &major) != 0)
1427 return 1;
1428
1429 /* Check major. */
1430 if (major >= 65536)
1431 return 1;
1432
1433 /* Convert minor. */
1434 minor = 0;
1435 if (p < end)
1436 if (strl2llrc(p + 1, end - (p + 1), &minor) != 0)
1437 return 1;
1438
1439 /* Check minor. */
1440 if (minor >= 65536)
1441 return 1;
1442
1443 /* Compose value. */
1444 *ret = (major << 16) | (minor & 0xffff);
1445 return 0;
1446}
1447
Willy Tarreaua0d37b62007-12-02 22:00:35 +01001448/* This function parses a time value optionally followed by a unit suffix among
1449 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
1450 * expected by the caller. The computation does its best to avoid overflows.
1451 * The value is returned in <ret> if everything is fine, and a NULL is returned
1452 * by the function. In case of error, a pointer to the error is returned and
1453 * <ret> is left untouched. Values are automatically rounded up when needed.
1454 */
1455const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
1456{
1457 unsigned imult, idiv;
1458 unsigned omult, odiv;
1459 unsigned value;
1460
1461 omult = odiv = 1;
1462
1463 switch (unit_flags & TIME_UNIT_MASK) {
1464 case TIME_UNIT_US: omult = 1000000; break;
1465 case TIME_UNIT_MS: omult = 1000; break;
1466 case TIME_UNIT_S: break;
1467 case TIME_UNIT_MIN: odiv = 60; break;
1468 case TIME_UNIT_HOUR: odiv = 3600; break;
1469 case TIME_UNIT_DAY: odiv = 86400; break;
1470 default: break;
1471 }
1472
1473 value = 0;
1474
1475 while (1) {
1476 unsigned int j;
1477
1478 j = *text - '0';
1479 if (j > 9)
1480 break;
1481 text++;
1482 value *= 10;
1483 value += j;
1484 }
1485
1486 imult = idiv = 1;
1487 switch (*text) {
1488 case '\0': /* no unit = default unit */
1489 imult = omult = idiv = odiv = 1;
1490 break;
1491 case 's': /* second = unscaled unit */
1492 break;
1493 case 'u': /* microsecond : "us" */
1494 if (text[1] == 's') {
1495 idiv = 1000000;
1496 text++;
1497 }
1498 break;
1499 case 'm': /* millisecond : "ms" or minute: "m" */
1500 if (text[1] == 's') {
1501 idiv = 1000;
1502 text++;
1503 } else
1504 imult = 60;
1505 break;
1506 case 'h': /* hour : "h" */
1507 imult = 3600;
1508 break;
1509 case 'd': /* day : "d" */
1510 imult = 86400;
1511 break;
1512 default:
1513 return text;
1514 break;
1515 }
1516
1517 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
1518 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
1519 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
1520 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
1521
1522 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
1523 *ret = value;
1524 return NULL;
1525}
Willy Tarreau6911fa42007-03-04 18:06:08 +01001526
Emeric Brun39132b22010-01-04 14:57:24 +01001527/* this function converts the string starting at <text> to an unsigned int
1528 * stored in <ret>. If an error is detected, the pointer to the unexpected
1529 * character is returned. If the conversio is succesful, NULL is returned.
1530 */
1531const char *parse_size_err(const char *text, unsigned *ret) {
1532 unsigned value = 0;
1533
1534 while (1) {
1535 unsigned int j;
1536
1537 j = *text - '0';
1538 if (j > 9)
1539 break;
1540 if (value > ~0U / 10)
1541 return text;
1542 value *= 10;
1543 if (value > (value + j))
1544 return text;
1545 value += j;
1546 text++;
1547 }
1548
1549 switch (*text) {
1550 case '\0':
1551 break;
1552 case 'K':
1553 case 'k':
1554 if (value > ~0U >> 10)
1555 return text;
1556 value = value << 10;
1557 break;
1558 case 'M':
1559 case 'm':
1560 if (value > ~0U >> 20)
1561 return text;
1562 value = value << 20;
1563 break;
1564 case 'G':
1565 case 'g':
1566 if (value > ~0U >> 30)
1567 return text;
1568 value = value << 30;
1569 break;
1570 default:
1571 return text;
1572 }
1573
1574 *ret = value;
1575 return NULL;
1576}
1577
Willy Tarreau126d4062013-12-03 17:50:47 +01001578/*
1579 * Parse binary string written in hexadecimal (source) and store the decoded
1580 * result into binstr and set binstrlen to the lengh of binstr. Memory for
1581 * binstr is allocated by the function. In case of error, returns 0 with an
Thierry FOURNIERee330af2014-01-21 11:36:14 +01001582 * error message in err. In succes case, it returns the consumed length.
Willy Tarreau126d4062013-12-03 17:50:47 +01001583 */
1584int parse_binary(const char *source, char **binstr, int *binstrlen, char **err)
1585{
1586 int len;
1587 const char *p = source;
1588 int i,j;
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001589 int alloc;
Willy Tarreau126d4062013-12-03 17:50:47 +01001590
1591 len = strlen(source);
1592 if (len % 2) {
1593 memprintf(err, "an even number of hex digit is expected");
1594 return 0;
1595 }
1596
1597 len = len >> 1;
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001598
Willy Tarreau126d4062013-12-03 17:50:47 +01001599 if (!*binstr) {
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001600 *binstr = calloc(len, sizeof(char));
1601 if (!*binstr) {
1602 memprintf(err, "out of memory while loading string pattern");
1603 return 0;
1604 }
1605 alloc = 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001606 }
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001607 else {
1608 if (*binstrlen < len) {
1609 memprintf(err, "no space avalaible in the buffer. expect %d, provides %d",
1610 len, *binstrlen);
1611 return 0;
1612 }
1613 alloc = 0;
1614 }
1615 *binstrlen = len;
Willy Tarreau126d4062013-12-03 17:50:47 +01001616
1617 i = j = 0;
1618 while (j < len) {
1619 if (!ishex(p[i++]))
1620 goto bad_input;
1621 if (!ishex(p[i++]))
1622 goto bad_input;
1623 (*binstr)[j++] = (hex2i(p[i-2]) << 4) + hex2i(p[i-1]);
1624 }
Thierry FOURNIERee330af2014-01-21 11:36:14 +01001625 return len << 1;
Willy Tarreau126d4062013-12-03 17:50:47 +01001626
1627bad_input:
1628 memprintf(err, "an hex digit is expected (found '%c')", p[i-1]);
Thierry FOURNIER9645d422013-12-06 19:59:28 +01001629 if (alloc)
1630 free(binstr);
Willy Tarreau126d4062013-12-03 17:50:47 +01001631 return 0;
1632}
1633
Willy Tarreau946ba592009-05-10 15:41:18 +02001634/* copies at most <n> characters from <src> and always terminates with '\0' */
1635char *my_strndup(const char *src, int n)
1636{
1637 int len = 0;
1638 char *ret;
1639
1640 while (len < n && src[len])
1641 len++;
1642
1643 ret = (char *)malloc(len + 1);
1644 if (!ret)
1645 return ret;
1646 memcpy(ret, src, len);
1647 ret[len] = '\0';
1648 return ret;
1649}
1650
Baptiste Assmannbb77c8e2013-10-06 23:24:13 +02001651/*
1652 * search needle in haystack
1653 * returns the pointer if found, returns NULL otherwise
1654 */
1655const void *my_memmem(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen)
1656{
1657 const void *c = NULL;
1658 unsigned char f;
1659
1660 if ((haystack == NULL) || (needle == NULL) || (haystacklen < needlelen))
1661 return NULL;
1662
1663 f = *(char *)needle;
1664 c = haystack;
1665 while ((c = memchr(c, f, haystacklen - (c - haystack))) != NULL) {
1666 if ((haystacklen - (c - haystack)) < needlelen)
1667 return NULL;
1668
1669 if (memcmp(c, needle, needlelen) == 0)
1670 return c;
1671 ++c;
1672 }
1673 return NULL;
1674}
1675
Willy Tarreau482b00d2009-10-04 22:48:42 +02001676/* This function returns the first unused key greater than or equal to <key> in
1677 * ID tree <root>. Zero is returned if no place is found.
1678 */
1679unsigned int get_next_id(struct eb_root *root, unsigned int key)
1680{
1681 struct eb32_node *used;
1682
1683 do {
1684 used = eb32_lookup_ge(root, key);
1685 if (!used || used->key > key)
1686 return key; /* key is available */
1687 key++;
1688 } while (key);
1689 return key;
1690}
1691
Willy Tarreau348238b2010-01-18 15:05:57 +01001692/* This function compares a sample word possibly followed by blanks to another
1693 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
1694 * otherwise zero. This intends to be used when checking HTTP headers for some
1695 * values. Note that it validates a word followed only by blanks but does not
1696 * validate a word followed by blanks then other chars.
1697 */
1698int word_match(const char *sample, int slen, const char *word, int wlen)
1699{
1700 if (slen < wlen)
1701 return 0;
1702
1703 while (wlen) {
1704 char c = *sample ^ *word;
1705 if (c && c != ('A' ^ 'a'))
1706 return 0;
1707 sample++;
1708 word++;
1709 slen--;
1710 wlen--;
1711 }
1712
1713 while (slen) {
1714 if (*sample != ' ' && *sample != '\t')
1715 return 0;
1716 sample++;
1717 slen--;
1718 }
1719 return 1;
1720}
Willy Tarreau482b00d2009-10-04 22:48:42 +02001721
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001722/* Converts any text-formatted IPv4 address to a host-order IPv4 address. It
1723 * is particularly fast because it avoids expensive operations such as
1724 * multiplies, which are optimized away at the end. It requires a properly
1725 * formated address though (3 points).
1726 */
1727unsigned int inetaddr_host(const char *text)
1728{
1729 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1730 register unsigned int dig100, dig10, dig1;
1731 int s;
1732 const char *p, *d;
1733
1734 dig1 = dig10 = dig100 = ascii_zero;
1735 s = 24;
1736
1737 p = text;
1738 while (1) {
1739 if (((unsigned)(*p - '0')) <= 9) {
1740 p++;
1741 continue;
1742 }
1743
1744 /* here, we have a complete byte between <text> and <p> (exclusive) */
1745 if (p == text)
1746 goto end;
1747
1748 d = p - 1;
1749 dig1 |= (unsigned int)(*d << s);
1750 if (d == text)
1751 goto end;
1752
1753 d--;
1754 dig10 |= (unsigned int)(*d << s);
1755 if (d == text)
1756 goto end;
1757
1758 d--;
1759 dig100 |= (unsigned int)(*d << s);
1760 end:
1761 if (!s || *p != '.')
1762 break;
1763
1764 s -= 8;
1765 text = ++p;
1766 }
1767
1768 dig100 -= ascii_zero;
1769 dig10 -= ascii_zero;
1770 dig1 -= ascii_zero;
1771 return ((dig100 * 10) + dig10) * 10 + dig1;
1772}
1773
1774/*
1775 * Idem except the first unparsed character has to be passed in <stop>.
1776 */
1777unsigned int inetaddr_host_lim(const char *text, const char *stop)
1778{
1779 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1780 register unsigned int dig100, dig10, dig1;
1781 int s;
1782 const char *p, *d;
1783
1784 dig1 = dig10 = dig100 = ascii_zero;
1785 s = 24;
1786
1787 p = text;
1788 while (1) {
1789 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1790 p++;
1791 continue;
1792 }
1793
1794 /* here, we have a complete byte between <text> and <p> (exclusive) */
1795 if (p == text)
1796 goto end;
1797
1798 d = p - 1;
1799 dig1 |= (unsigned int)(*d << s);
1800 if (d == text)
1801 goto end;
1802
1803 d--;
1804 dig10 |= (unsigned int)(*d << s);
1805 if (d == text)
1806 goto end;
1807
1808 d--;
1809 dig100 |= (unsigned int)(*d << s);
1810 end:
1811 if (!s || p == stop || *p != '.')
1812 break;
1813
1814 s -= 8;
1815 text = ++p;
1816 }
1817
1818 dig100 -= ascii_zero;
1819 dig10 -= ascii_zero;
1820 dig1 -= ascii_zero;
1821 return ((dig100 * 10) + dig10) * 10 + dig1;
1822}
1823
1824/*
1825 * Idem except the pointer to first unparsed byte is returned into <ret> which
1826 * must not be NULL.
1827 */
Willy Tarreau74172752010-10-15 23:21:42 +02001828unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret)
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001829{
1830 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1831 register unsigned int dig100, dig10, dig1;
1832 int s;
Willy Tarreau74172752010-10-15 23:21:42 +02001833 char *p, *d;
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001834
1835 dig1 = dig10 = dig100 = ascii_zero;
1836 s = 24;
1837
1838 p = text;
1839 while (1) {
1840 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1841 p++;
1842 continue;
1843 }
1844
1845 /* here, we have a complete byte between <text> and <p> (exclusive) */
1846 if (p == text)
1847 goto end;
1848
1849 d = p - 1;
1850 dig1 |= (unsigned int)(*d << s);
1851 if (d == text)
1852 goto end;
1853
1854 d--;
1855 dig10 |= (unsigned int)(*d << s);
1856 if (d == text)
1857 goto end;
1858
1859 d--;
1860 dig100 |= (unsigned int)(*d << s);
1861 end:
1862 if (!s || p == stop || *p != '.')
1863 break;
1864
1865 s -= 8;
1866 text = ++p;
1867 }
1868
1869 *ret = p;
1870 dig100 -= ascii_zero;
1871 dig10 -= ascii_zero;
1872 dig1 -= ascii_zero;
1873 return ((dig100 * 10) + dig10) * 10 + dig1;
1874}
1875
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001876/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
1877 * or the number of chars read in case of success. Maybe this could be replaced
1878 * by one of the functions above. Also, apparently this function does not support
1879 * hosts above 255 and requires exactly 4 octets.
Willy Tarreau075415a2013-12-12 11:29:39 +01001880 * The destination is only modified on success.
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001881 */
1882int buf2ip(const char *buf, size_t len, struct in_addr *dst)
1883{
1884 const char *addr;
1885 int saw_digit, octets, ch;
1886 u_char tmp[4], *tp;
1887 const char *cp = buf;
1888
1889 saw_digit = 0;
1890 octets = 0;
1891 *(tp = tmp) = 0;
1892
1893 for (addr = buf; addr - buf < len; addr++) {
1894 unsigned char digit = (ch = *addr) - '0';
1895
1896 if (digit > 9 && ch != '.')
1897 break;
1898
1899 if (digit <= 9) {
1900 u_int new = *tp * 10 + digit;
1901
1902 if (new > 255)
1903 return 0;
1904
1905 *tp = new;
1906
1907 if (!saw_digit) {
1908 if (++octets > 4)
1909 return 0;
1910 saw_digit = 1;
1911 }
1912 } else if (ch == '.' && saw_digit) {
1913 if (octets == 4)
1914 return 0;
1915
1916 *++tp = 0;
1917 saw_digit = 0;
1918 } else
1919 return 0;
1920 }
1921
1922 if (octets < 4)
1923 return 0;
1924
1925 memcpy(&dst->s_addr, tmp, 4);
1926 return addr - cp;
1927}
1928
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01001929/* This function converts the string in <buf> of the len <len> to
1930 * struct in6_addr <dst> which must be allocated by the caller.
1931 * This function returns 1 in success case, otherwise zero.
Willy Tarreau075415a2013-12-12 11:29:39 +01001932 * The destination is only modified on success.
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01001933 */
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01001934int buf2ip6(const char *buf, size_t len, struct in6_addr *dst)
1935{
Thierry FOURNIERcd659912013-12-11 12:33:54 +01001936 char null_term_ip6[INET6_ADDRSTRLEN + 1];
Willy Tarreau075415a2013-12-12 11:29:39 +01001937 struct in6_addr out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01001938
Thierry FOURNIERcd659912013-12-11 12:33:54 +01001939 if (len > INET6_ADDRSTRLEN)
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01001940 return 0;
1941
1942 memcpy(null_term_ip6, buf, len);
1943 null_term_ip6[len] = '\0';
1944
Willy Tarreau075415a2013-12-12 11:29:39 +01001945 if (!inet_pton(AF_INET6, null_term_ip6, &out))
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01001946 return 0;
1947
Willy Tarreau075415a2013-12-12 11:29:39 +01001948 *dst = out;
Thierry FOURNIERd559dd82013-11-22 16:16:59 +01001949 return 1;
1950}
1951
Willy Tarreauacf95772010-06-14 19:09:21 +02001952/* To be used to quote config arg positions. Returns the short string at <ptr>
1953 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
1954 * if ptr is NULL or empty. The string is locally allocated.
1955 */
1956const char *quote_arg(const char *ptr)
1957{
1958 static char val[32];
1959 int i;
1960
1961 if (!ptr || !*ptr)
1962 return "end of line";
1963 val[0] = '\'';
Willy Tarreaude2dd6b2013-01-24 02:14:42 +01001964 for (i = 1; i < sizeof(val) - 2 && *ptr; i++)
Willy Tarreauacf95772010-06-14 19:09:21 +02001965 val[i] = *ptr++;
1966 val[i++] = '\'';
1967 val[i] = '\0';
1968 return val;
1969}
1970
Willy Tarreau5b180202010-07-18 10:40:48 +02001971/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
1972int get_std_op(const char *str)
1973{
1974 int ret = -1;
1975
1976 if (*str == 'e' && str[1] == 'q')
1977 ret = STD_OP_EQ;
1978 else if (*str == 'n' && str[1] == 'e')
1979 ret = STD_OP_NE;
1980 else if (*str == 'l') {
1981 if (str[1] == 'e') ret = STD_OP_LE;
1982 else if (str[1] == 't') ret = STD_OP_LT;
1983 }
1984 else if (*str == 'g') {
1985 if (str[1] == 'e') ret = STD_OP_GE;
1986 else if (str[1] == 't') ret = STD_OP_GT;
1987 }
1988
1989 if (ret == -1 || str[2] != '\0')
1990 return -1;
1991 return ret;
1992}
1993
Willy Tarreau4c14eaa2010-11-24 14:01:45 +01001994/* hash a 32-bit integer to another 32-bit integer */
1995unsigned int full_hash(unsigned int a)
1996{
1997 return __full_hash(a);
1998}
1999
David du Colombier4f92d322011-03-24 11:09:31 +01002000/* Return non-zero if IPv4 address is part of the network,
2001 * otherwise zero.
2002 */
2003int in_net_ipv4(struct in_addr *addr, struct in_addr *mask, struct in_addr *net)
2004{
2005 return((addr->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr));
2006}
2007
2008/* Return non-zero if IPv6 address is part of the network,
2009 * otherwise zero.
2010 */
2011int in_net_ipv6(struct in6_addr *addr, struct in6_addr *mask, struct in6_addr *net)
2012{
2013 int i;
2014
2015 for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++)
2016 if (((((int *)addr)[i] & ((int *)mask)[i])) !=
2017 (((int *)net)[i] & ((int *)mask)[i]))
2018 return 0;
2019 return 1;
2020}
2021
2022/* RFC 4291 prefix */
2023const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00,
2024 0x00, 0x00, 0x00, 0x00,
2025 0x00, 0x00, 0xFF, 0xFF };
2026
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002027/* Map IPv4 adress on IPv6 address, as specified in RFC 3513.
2028 * Input and output may overlap.
2029 */
David du Colombier4f92d322011-03-24 11:09:31 +01002030void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr)
2031{
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002032 struct in_addr tmp_addr;
2033
2034 tmp_addr.s_addr = sin_addr->s_addr;
David du Colombier4f92d322011-03-24 11:09:31 +01002035 memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx));
Thierry FOURNIER4a04dc32013-11-28 16:33:15 +01002036 memcpy(sin6_addr->s6_addr+12, &tmp_addr.s_addr, 4);
David du Colombier4f92d322011-03-24 11:09:31 +01002037}
2038
2039/* Map IPv6 adress on IPv4 address, as specified in RFC 3513.
2040 * Return true if conversion is possible and false otherwise.
2041 */
2042int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr)
2043{
2044 if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) {
2045 memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]),
2046 sizeof(struct in_addr));
2047 return 1;
2048 }
2049
2050 return 0;
2051}
2052
William Lallemand421f5b52012-02-06 18:15:57 +01002053char *human_time(int t, short hz_div) {
2054 static char rv[sizeof("24855d23h")+1]; // longest of "23h59m" and "59m59s"
2055 char *p = rv;
Willy Tarreau761b3d52014-04-14 14:53:06 +02002056 char *end = rv + sizeof(rv);
William Lallemand421f5b52012-02-06 18:15:57 +01002057 int cnt=2; // print two numbers
2058
2059 if (unlikely(t < 0 || hz_div <= 0)) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002060 snprintf(p, end - p, "?");
William Lallemand421f5b52012-02-06 18:15:57 +01002061 return rv;
2062 }
2063
2064 if (unlikely(hz_div > 1))
2065 t /= hz_div;
2066
2067 if (t >= DAY) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002068 p += snprintf(p, end - p, "%dd", t / DAY);
William Lallemand421f5b52012-02-06 18:15:57 +01002069 cnt--;
2070 }
2071
2072 if (cnt && t % DAY / HOUR) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002073 p += snprintf(p, end - p, "%dh", t % DAY / HOUR);
William Lallemand421f5b52012-02-06 18:15:57 +01002074 cnt--;
2075 }
2076
2077 if (cnt && t % HOUR / MINUTE) {
Willy Tarreau761b3d52014-04-14 14:53:06 +02002078 p += snprintf(p, end - p, "%dm", t % HOUR / MINUTE);
William Lallemand421f5b52012-02-06 18:15:57 +01002079 cnt--;
2080 }
2081
2082 if ((cnt && t % MINUTE) || !t) // also display '0s'
Willy Tarreau761b3d52014-04-14 14:53:06 +02002083 p += snprintf(p, end - p, "%ds", t % MINUTE / SEC);
William Lallemand421f5b52012-02-06 18:15:57 +01002084
2085 return rv;
2086}
2087
2088const char *monthname[12] = {
2089 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
2090 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
2091};
2092
2093/* date2str_log: write a date in the format :
2094 * sprintf(str, "%02d/%s/%04d:%02d:%02d:%02d.%03d",
2095 * tm.tm_mday, monthname[tm.tm_mon], tm.tm_year+1900,
2096 * tm.tm_hour, tm.tm_min, tm.tm_sec, (int)date.tv_usec/1000);
2097 *
2098 * without using sprintf. return a pointer to the last char written (\0) or
2099 * NULL if there isn't enough space.
2100 */
2101char *date2str_log(char *dst, struct tm *tm, struct timeval *date, size_t size)
2102{
2103
2104 if (size < 25) /* the size is fixed: 24 chars + \0 */
2105 return NULL;
2106
2107 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2108 *dst++ = '/';
2109 memcpy(dst, monthname[tm->tm_mon], 3); // month
2110 dst += 3;
2111 *dst++ = '/';
2112 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2113 *dst++ = ':';
2114 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2115 *dst++ = ':';
2116 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2117 *dst++ = ':';
2118 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2119 *dst++ = '.';
2120 utoa_pad((unsigned int)(date->tv_usec/1000), dst, 4); // millisecondes
2121 dst += 3; // only the 3 first digits
2122 *dst = '\0';
2123
2124 return dst;
2125}
2126
2127/* gmt2str_log: write a date in the format :
2128 * "%02d/%s/%04d:%02d:%02d:%02d +0000" without using snprintf
2129 * return a pointer to the last char written (\0) or
2130 * NULL if there isn't enough space.
2131 */
2132char *gmt2str_log(char *dst, struct tm *tm, size_t size)
2133{
Yuxans Yao4e25b012012-10-19 10:36:09 +08002134 if (size < 27) /* the size is fixed: 26 chars + \0 */
William Lallemand421f5b52012-02-06 18:15:57 +01002135 return NULL;
2136
2137 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2138 *dst++ = '/';
2139 memcpy(dst, monthname[tm->tm_mon], 3); // month
2140 dst += 3;
2141 *dst++ = '/';
2142 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2143 *dst++ = ':';
2144 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2145 *dst++ = ':';
2146 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2147 *dst++ = ':';
2148 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2149 *dst++ = ' ';
2150 *dst++ = '+';
2151 *dst++ = '0';
2152 *dst++ = '0';
2153 *dst++ = '0';
2154 *dst++ = '0';
2155 *dst = '\0';
2156
2157 return dst;
2158}
2159
Yuxans Yao4e25b012012-10-19 10:36:09 +08002160/* localdate2str_log: write a date in the format :
2161 * "%02d/%s/%04d:%02d:%02d:%02d +0000(local timezone)" without using snprintf
2162 * * return a pointer to the last char written (\0) or
2163 * * NULL if there isn't enough space.
2164 */
2165char *localdate2str_log(char *dst, struct tm *tm, size_t size)
2166{
2167 if (size < 27) /* the size is fixed: 26 chars + \0 */
2168 return NULL;
2169
2170 dst = utoa_pad((unsigned int)tm->tm_mday, dst, 3); // day
2171 *dst++ = '/';
2172 memcpy(dst, monthname[tm->tm_mon], 3); // month
2173 dst += 3;
2174 *dst++ = '/';
2175 dst = utoa_pad((unsigned int)tm->tm_year+1900, dst, 5); // year
2176 *dst++ = ':';
2177 dst = utoa_pad((unsigned int)tm->tm_hour, dst, 3); // hour
2178 *dst++ = ':';
2179 dst = utoa_pad((unsigned int)tm->tm_min, dst, 3); // minutes
2180 *dst++ = ':';
2181 dst = utoa_pad((unsigned int)tm->tm_sec, dst, 3); // secondes
2182 *dst++ = ' ';
2183 memcpy(dst, localtimezone, 5); // timezone
2184 dst += 5;
2185 *dst = '\0';
2186
2187 return dst;
2188}
2189
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002190/* Dynamically allocates a string of the proper length to hold the formatted
2191 * output. NULL is returned on error. The caller is responsible for freeing the
2192 * memory area using free(). The resulting string is returned in <out> if the
2193 * pointer is not NULL. A previous version of <out> might be used to build the
2194 * new string, and it will be freed before returning if it is not NULL, which
2195 * makes it possible to build complex strings from iterative calls without
2196 * having to care about freeing intermediate values, as in the example below :
2197 *
2198 * memprintf(&err, "invalid argument: '%s'", arg);
2199 * ...
2200 * memprintf(&err, "parser said : <%s>\n", *err);
2201 * ...
2202 * free(*err);
2203 *
2204 * This means that <err> must be initialized to NULL before first invocation.
2205 * The return value also holds the allocated string, which eases error checking
2206 * and immediate consumption. If the output pointer is not used, NULL must be
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002207 * passed instead and it will be ignored. The returned message will then also
2208 * be NULL so that the caller does not have to bother with freeing anything.
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002209 *
2210 * It is also convenient to use it without any free except the last one :
2211 * err = NULL;
2212 * if (!fct1(err)) report(*err);
2213 * if (!fct2(err)) report(*err);
2214 * if (!fct3(err)) report(*err);
2215 * free(*err);
2216 */
2217char *memprintf(char **out, const char *format, ...)
2218{
2219 va_list args;
2220 char *ret = NULL;
2221 int allocated = 0;
2222 int needed = 0;
2223
Willy Tarreaueb6cead2012-09-20 19:43:14 +02002224 if (!out)
2225 return NULL;
2226
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002227 do {
2228 /* vsnprintf() will return the required length even when the
2229 * target buffer is NULL. We do this in a loop just in case
2230 * intermediate evaluations get wrong.
2231 */
2232 va_start(args, format);
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002233 needed = vsnprintf(ret, allocated, format, args);
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002234 va_end(args);
2235
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002236 if (needed < allocated) {
2237 /* Note: on Solaris 8, the first iteration always
2238 * returns -1 if allocated is zero, so we force a
2239 * retry.
2240 */
2241 if (!allocated)
2242 needed = 0;
2243 else
2244 break;
2245 }
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002246
Willy Tarreau1b2fed62013-04-01 22:48:54 +02002247 allocated = needed + 1;
Willy Tarreau9a7bea52012-04-27 11:16:50 +02002248 ret = realloc(ret, allocated);
2249 } while (ret);
2250
2251 if (needed < 0) {
2252 /* an error was encountered */
2253 free(ret);
2254 ret = NULL;
2255 }
2256
2257 if (out) {
2258 free(*out);
2259 *out = ret;
2260 }
2261
2262 return ret;
2263}
William Lallemand421f5b52012-02-06 18:15:57 +01002264
Willy Tarreau21c705b2012-09-14 11:40:36 +02002265/* Used to add <level> spaces before each line of <out>, unless there is only one line.
2266 * The input argument is automatically freed and reassigned. The result will have to be
Willy Tarreau70eec382012-10-10 08:56:47 +02002267 * freed by the caller. It also supports being passed a NULL which results in the same
2268 * output.
Willy Tarreau21c705b2012-09-14 11:40:36 +02002269 * Example of use :
2270 * parse(cmd, &err); (callee: memprintf(&err, ...))
2271 * fprintf(stderr, "Parser said: %s\n", indent_error(&err));
2272 * free(err);
2273 */
2274char *indent_msg(char **out, int level)
2275{
2276 char *ret, *in, *p;
2277 int needed = 0;
2278 int lf = 0;
2279 int lastlf = 0;
2280 int len;
2281
Willy Tarreau70eec382012-10-10 08:56:47 +02002282 if (!out || !*out)
2283 return NULL;
2284
Willy Tarreau21c705b2012-09-14 11:40:36 +02002285 in = *out - 1;
2286 while ((in = strchr(in + 1, '\n')) != NULL) {
2287 lastlf = in - *out;
2288 lf++;
2289 }
2290
2291 if (!lf) /* single line, no LF, return it as-is */
2292 return *out;
2293
2294 len = strlen(*out);
2295
2296 if (lf == 1 && lastlf == len - 1) {
2297 /* single line, LF at end, strip it and return as-is */
2298 (*out)[lastlf] = 0;
2299 return *out;
2300 }
2301
2302 /* OK now we have at least one LF, we need to process the whole string
2303 * as a multi-line string. What we'll do :
2304 * - prefix with an LF if there is none
2305 * - add <level> spaces before each line
2306 * This means at most ( 1 + level + (len-lf) + lf*<1+level) ) =
2307 * 1 + level + len + lf * level = 1 + level * (lf + 1) + len.
2308 */
2309
2310 needed = 1 + level * (lf + 1) + len + 1;
2311 p = ret = malloc(needed);
2312 in = *out;
2313
2314 /* skip initial LFs */
2315 while (*in == '\n')
2316 in++;
2317
2318 /* copy each line, prefixed with LF and <level> spaces, and without the trailing LF */
2319 while (*in) {
2320 *p++ = '\n';
2321 memset(p, ' ', level);
2322 p += level;
2323 do {
2324 *p++ = *in++;
2325 } while (*in && *in != '\n');
2326 if (*in)
2327 in++;
2328 }
2329 *p = 0;
2330
2331 free(*out);
2332 *out = ret;
2333
2334 return ret;
2335}
2336
Willy Tarreaudad36a32013-03-11 01:20:04 +01002337/* Convert occurrences of environment variables in the input string to their
2338 * corresponding value. A variable is identified as a series of alphanumeric
2339 * characters or underscores following a '$' sign. The <in> string must be
2340 * free()able. NULL returns NULL. The resulting string might be reallocated if
2341 * some expansion is made. Variable names may also be enclosed into braces if
2342 * needed (eg: to concatenate alphanum characters).
2343 */
2344char *env_expand(char *in)
2345{
2346 char *txt_beg;
2347 char *out;
2348 char *txt_end;
2349 char *var_beg;
2350 char *var_end;
2351 char *value;
2352 char *next;
2353 int out_len;
2354 int val_len;
2355
2356 if (!in)
2357 return in;
2358
2359 value = out = NULL;
2360 out_len = 0;
2361
2362 txt_beg = in;
2363 do {
2364 /* look for next '$' sign in <in> */
2365 for (txt_end = txt_beg; *txt_end && *txt_end != '$'; txt_end++);
2366
2367 if (!*txt_end && !out) /* end and no expansion performed */
2368 return in;
2369
2370 val_len = 0;
2371 next = txt_end;
2372 if (*txt_end == '$') {
2373 char save;
2374
2375 var_beg = txt_end + 1;
2376 if (*var_beg == '{')
2377 var_beg++;
2378
2379 var_end = var_beg;
2380 while (isalnum((int)(unsigned char)*var_end) || *var_end == '_') {
2381 var_end++;
2382 }
2383
2384 next = var_end;
2385 if (*var_end == '}' && (var_beg > txt_end + 1))
2386 next++;
2387
2388 /* get value of the variable name at this location */
2389 save = *var_end;
2390 *var_end = '\0';
2391 value = getenv(var_beg);
2392 *var_end = save;
2393 val_len = value ? strlen(value) : 0;
2394 }
2395
2396 out = realloc(out, out_len + (txt_end - txt_beg) + val_len + 1);
2397 if (txt_end > txt_beg) {
2398 memcpy(out + out_len, txt_beg, txt_end - txt_beg);
2399 out_len += txt_end - txt_beg;
2400 }
2401 if (val_len) {
2402 memcpy(out + out_len, value, val_len);
2403 out_len += val_len;
2404 }
2405 out[out_len] = 0;
2406 txt_beg = next;
2407 } while (*txt_beg);
2408
2409 /* here we know that <out> was allocated and that we don't need <in> anymore */
2410 free(in);
2411 return out;
2412}
2413
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002414
2415/* same as strstr() but case-insensitive and with limit length */
2416const char *strnistr(const char *str1, int len_str1, const char *str2, int len_str2)
2417{
2418 char *pptr, *sptr, *start;
Willy Tarreauc8746532014-05-28 23:05:07 +02002419 unsigned int slen, plen;
2420 unsigned int tmp1, tmp2;
de Lafond Guillaume88c278f2013-04-15 19:27:10 +02002421
2422 if (str1 == NULL || len_str1 == 0) // search pattern into an empty string => search is not found
2423 return NULL;
2424
2425 if (str2 == NULL || len_str2 == 0) // pattern is empty => every str1 match
2426 return str1;
2427
2428 if (len_str1 < len_str2) // pattern is longer than string => search is not found
2429 return NULL;
2430
2431 for (tmp1 = 0, start = (char *)str1, pptr = (char *)str2, slen = len_str1, plen = len_str2; slen >= plen; start++, slen--) {
2432 while (toupper(*start) != toupper(*str2)) {
2433 start++;
2434 slen--;
2435 tmp1++;
2436
2437 if (tmp1 >= len_str1)
2438 return NULL;
2439
2440 /* if pattern longer than string */
2441 if (slen < plen)
2442 return NULL;
2443 }
2444
2445 sptr = start;
2446 pptr = (char *)str2;
2447
2448 tmp2 = 0;
2449 while (toupper(*sptr) == toupper(*pptr)) {
2450 sptr++;
2451 pptr++;
2452 tmp2++;
2453
2454 if (*pptr == '\0' || tmp2 == len_str2) /* end of pattern found */
2455 return start;
2456 if (*sptr == '\0' || tmp2 == len_str1) /* end of string found and the pattern is not fully found */
2457 return NULL;
2458 }
2459 }
2460 return NULL;
2461}
2462
Willy Tarreaubaaee002006-06-26 02:48:02 +02002463/*
2464 * Local variables:
2465 * c-indent-level: 8
2466 * c-basic-offset: 8
2467 * End:
2468 */