blob: 647a6c8f6b563a644dd85c0ef8af7c7287ba1d51 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General purpose functions.
3 *
Willy Tarreau6911fa42007-03-04 18:06:08 +01004 * Copyright 2000-2007 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>
15#include <stdlib.h>
16#include <string.h>
17#include <netinet/in.h>
18#include <arpa/inet.h>
19
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020021#include <common/standard.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <proto/log.h>
23
Willy Tarreau72d759c2007-10-25 12:14:10 +020024/* enough to store 10 integers of :
25 * 2^64-1 = 18446744073709551615 or
26 * -2^63 = -9223372036854775808
27 */
28char itoa_str[10][21];
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
30/*
31 * copies at most <size-1> chars from <src> to <dst>. Last char is always
32 * set to 0, unless <size> is 0. The number of chars copied is returned
33 * (excluding the terminating zero).
34 * This code has been optimized for size and speed : on x86, it's 45 bytes
35 * long, uses only registers, and consumes only 4 cycles per char.
36 */
37int strlcpy2(char *dst, const char *src, int size)
38{
39 char *orig = dst;
40 if (size) {
41 while (--size && (*dst = *src)) {
42 src++; dst++;
43 }
44 *dst = 0;
45 }
46 return dst - orig;
47}
48
49/*
Willy Tarreau72d759c2007-10-25 12:14:10 +020050 * This function simply returns a locally allocated string containing
Willy Tarreaubaaee002006-06-26 02:48:02 +020051 * the ascii representation for number 'n' in decimal.
52 */
Willy Tarreau72d759c2007-10-25 12:14:10 +020053const char *ultoa_r(unsigned long n, char *buffer, int size)
Willy Tarreaubaaee002006-06-26 02:48:02 +020054{
55 char *pos;
56
Willy Tarreau72d759c2007-10-25 12:14:10 +020057 pos = buffer + size - 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 *pos-- = '\0';
59
60 do {
61 *pos-- = '0' + n % 10;
62 n /= 10;
Willy Tarreau72d759c2007-10-25 12:14:10 +020063 } while (n && pos >= buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +020064 return pos + 1;
65}
66
Willy Tarreau91092e52007-10-25 16:58:42 +020067/*
68 * This function simply returns a locally allocated string containing the ascii
69 * representation for number 'n' in decimal, unless n is 0 in which case it
70 * returns the alternate string (or an empty string if the alternate string is
71 * NULL). It use is intended for limits reported in reports, where it's
72 * desirable not to display anything if there is no limit. Warning! it shares
73 * the same vector as ultoa_r().
74 */
75const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
76{
77 return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
78}
79
Robert Tsai81ae1952007-12-05 10:47:29 +010080/*
81 * converts <str> to a struct sockaddr_un* which is locally allocated.
82 * The format is "/path", where "/path" is a path to a UNIX domain socket.
83 */
84struct sockaddr_un *str2sun(char *str)
85{
86 static struct sockaddr_un sun;
87 int strsz; /* length included null */
88
89 memset(&sun, 0, sizeof(sun));
90 str = strdup(str);
91 if (str == NULL)
92 goto out_nofree;
93
94 strsz = strlen(str) + 1;
95 if (strsz > sizeof(sun.sun_path)) {
96 Alert("Socket path '%s' too long (max %d)\n",
97 str, sizeof(sun.sun_path) - 1);
98 goto out_nofree;
99 }
100
101#ifndef __SOCKADDR_COMMON
102 sun.sun_len = sizeof(sun);
103#endif /* !__SOCKADDR_COMMON */
104 sun.sun_family = AF_UNIX;
105 memcpy(sun.sun_path, str, strsz);
106
107 free(str);
108 out_nofree:
109 return &sun;
110}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200111
112/*
113 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
114 *
115 * It looks like this one would be a good candidate for inlining, but this is
116 * not interesting because it around 35 bytes long and often called multiple
117 * times within the same function.
118 */
119int ishex(char s)
120{
121 s -= '0';
122 if ((unsigned char)s <= 9)
123 return 1;
124 s -= 'A' - '0';
125 if ((unsigned char)s <= 5)
126 return 1;
127 s -= 'a' - 'A';
128 if ((unsigned char)s <= 5)
129 return 1;
130 return 0;
131}
132
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100133/*
134 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
135 * invalid character is found, a pointer to it is returned. If everything is
136 * fine, NULL is returned.
137 */
138const char *invalid_char(const char *name)
139{
140 if (!*name)
141 return name;
142
143 while (*name) {
144 if (!isalnum(*name) && *name != '.' && *name != ':' &&
145 *name != '_' && *name != '-')
146 return name;
147 name++;
148 }
149 return NULL;
150}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151
152/*
153 * converts <str> to a struct sockaddr_in* which is locally allocated.
154 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
155 * a host name, or empty or "*" to indicate INADDR_ANY.
156 */
157struct sockaddr_in *str2sa(char *str)
158{
159 static struct sockaddr_in sa;
160 char *c;
161 int port;
162
163 memset(&sa, 0, sizeof(sa));
164 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200165 if (str == NULL)
166 goto out_nofree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200167
168 if ((c = strrchr(str,':')) != NULL) {
169 *c++ = '\0';
170 port = atol(c);
171 }
172 else
173 port = 0;
174
175 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
176 sa.sin_addr.s_addr = INADDR_ANY;
177 }
178 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
179 struct hostent *he;
180
181 if ((he = gethostbyname(str)) == NULL) {
182 Alert("Invalid server name: '%s'\n", str);
183 }
184 else
185 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
186 }
Robert Tsai81ae1952007-12-05 10:47:29 +0100187#ifndef __SOCKADDR_COMMON
188 sa.sin_len = sizeof(sa);
189#endif /* !__SOCKADDR_COMMON */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190 sa.sin_port = htons(port);
191 sa.sin_family = AF_INET;
192
193 free(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200194 out_nofree:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195 return &sa;
196}
197
198/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200199 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200200 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
201 * is optionnal and either in the dotted or CIDR notation.
202 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
203 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200204int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200206 __label__ out_free, out_err;
207 char *c, *s;
208 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200209 unsigned long len;
210
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200211 s = strdup(str);
212 if (!s)
213 return 0;
214
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215 memset(mask, 0, sizeof(*mask));
216 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200218 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200219 *c++ = '\0';
220 /* c points to the mask */
221 if (strchr(c, '.') != NULL) { /* dotted notation */
222 if (!inet_pton(AF_INET, c, mask))
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200223 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224 }
225 else { /* mask length */
226 char *err;
227 len = strtol(c, &err, 10);
228 if (!*c || (err && *err) || (unsigned)len > 32)
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200229 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200230 if (len)
231 mask->s_addr = htonl(~0UL << (32 - len));
232 else
233 mask->s_addr = 0;
234 }
235 }
236 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100237 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200238 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200239 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240 struct hostent *he;
241
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200242 if ((he = gethostbyname(s)) == NULL) {
243 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244 }
245 else
246 *addr = *(struct in_addr *) *(he->h_addr_list);
247 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200248
249 ret_val = 1;
250 out_free:
251 free(s);
252 return ret_val;
253 out_err:
254 ret_val = 0;
255 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256}
257
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100258
259/*
260 * Parse IP address found in url.
261 */
262static int url2ip(const char *addr, struct in_addr *dst)
263{
264 int saw_digit, octets, ch;
265 u_char tmp[4], *tp;
266 const char *cp = addr;
267
268 saw_digit = 0;
269 octets = 0;
270 *(tp = tmp) = 0;
271
272 while (*addr) {
273 unsigned char digit = (ch = *addr++) - '0';
274 if (digit > 9 && ch != '.')
275 break;
276 if (digit <= 9) {
277 u_int new = *tp * 10 + digit;
278 if (new > 255)
279 return 0;
280 *tp = new;
281 if (!saw_digit) {
282 if (++octets > 4)
283 return 0;
284 saw_digit = 1;
285 }
286 } else if (ch == '.' && saw_digit) {
287 if (octets == 4)
288 return 0;
289 *++tp = 0;
290 saw_digit = 0;
291 } else
292 return 0;
293 }
294
295 if (octets < 4)
296 return 0;
297
298 memcpy(&dst->s_addr, tmp, 4);
299 return addr-cp-1;
300}
301
302/*
303 * Resolve destination server from URL. Convert <str> to a sockaddr_in*.
304 */
305int url2sa(const char *url, int ulen, struct sockaddr_in *addr)
306{
307 const char *curr = url, *cp = url;
308 int ret, url_code = 0;
309 unsigned int http_code = 0;
310
311 /* Cleanup the room */
312 addr->sin_family = AF_INET;
313 addr->sin_addr.s_addr = 0;
314 addr->sin_port = 0;
315
316 /* Firstly, try to find :// pattern */
317 while (curr < url+ulen && url_code != 0x3a2f2f) {
318 url_code = ((url_code & 0xffff) << 8);
319 url_code += (unsigned char)*curr++;
320 }
321
322 /* Secondly, if :// pattern is found, verify parsed stuff
323 * before pattern is matching our http pattern.
324 * If so parse ip address and port in uri.
325 *
326 * WARNING: Current code doesn't support dynamic async dns resolver.
327 */
328 if (url_code == 0x3a2f2f) {
329 while (cp < curr - 3)
330 http_code = (http_code << 8) + *cp++;
331 http_code |= 0x20202020; /* Turn everything to lower case */
332
333 /* HTTP url matching */
334 if (http_code == 0x68747470) {
335 /* We are looking for IP address. If you want to parse and
336 * resolve hostname found in url, you can use str2sa(), but
337 * be warned this can slow down global daemon performances
338 * while handling lagging dns responses.
339 */
340 ret = url2ip(curr, &addr->sin_addr);
341 if (!ret)
342 return -1;
343 curr += ret;
Willy Tarreaud1cd2762007-12-02 10:55:56 +0100344 addr->sin_port = (*curr == ':') ? str2uic(++curr) : 80;
345 addr->sin_port = htons(addr->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100346 }
347 return 0;
348 }
349
350 return -1;
351}
352
Willy Tarreaubaaee002006-06-26 02:48:02 +0200353/* will try to encode the string <string> replacing all characters tagged in
354 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
355 * prefixed by <escape>, and will store the result between <start> (included)
356 * and <stop> (excluded), and will always terminate the string with a '\0'
357 * before <stop>. The position of the '\0' is returned if the conversion
358 * completes. If bytes are missing between <start> and <stop>, then the
359 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
360 * cannot even be stored so we return <start> without writing the 0.
361 * The input string must also be zero-terminated.
362 */
363const char hextab[16] = "0123456789ABCDEF";
364char *encode_string(char *start, char *stop,
365 const char escape, const fd_set *map,
366 const char *string)
367{
368 if (start < stop) {
369 stop--; /* reserve one byte for the final '\0' */
370 while (start < stop && *string != '\0') {
371 if (!FD_ISSET((unsigned char)(*string), map))
372 *start++ = *string;
373 else {
374 if (start + 3 >= stop)
375 break;
376 *start++ = escape;
377 *start++ = hextab[(*string >> 4) & 15];
378 *start++ = hextab[*string & 15];
379 }
380 string++;
381 }
382 *start = '\0';
383 }
384 return start;
385}
386
387
Willy Tarreau6911fa42007-03-04 18:06:08 +0100388unsigned int str2ui(const char *s)
389{
390 return __str2ui(s);
391}
392
393unsigned int str2uic(const char *s)
394{
395 return __str2uic(s);
396}
397
398unsigned int strl2ui(const char *s, int len)
399{
400 return __strl2ui(s, len);
401}
402
403unsigned int strl2uic(const char *s, int len)
404{
405 return __strl2uic(s, len);
406}
407
408/* This one is 7 times faster than strtol() on athlon with checks.
409 * It returns the value of the number composed of all valid digits read,
410 * and can process negative numbers too.
411 */
412int strl2ic(const char *s, int len)
413{
414 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200415 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100416
417 if (len > 0) {
418 if (*s != '-') {
419 /* positive number */
420 while (len-- > 0) {
421 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200422 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100423 if (j > 9)
424 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200425 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100426 }
427 } else {
428 /* negative number */
429 s++;
430 while (--len > 0) {
431 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200432 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100433 if (j > 9)
434 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200435 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100436 }
437 }
438 }
439 return i;
440}
441
442
443/* This function reads exactly <len> chars from <s> and converts them to a
444 * signed integer which it stores into <ret>. It accurately detects any error
445 * (truncated string, invalid chars, overflows). It is meant to be used in
446 * applications designed for hostile environments. It returns zero when the
447 * number has successfully been converted, non-zero otherwise. When an error
448 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
449 * faster than strtol().
450 */
451int strl2irc(const char *s, int len, int *ret)
452{
453 int i = 0;
454 int j;
455
456 if (!len)
457 return 1;
458
459 if (*s != '-') {
460 /* positive number */
461 while (len-- > 0) {
462 j = (*s++) - '0';
463 if (j > 9) return 1; /* invalid char */
464 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
465 i = i * 10;
466 if (i + j < i) return 1; /* check for addition overflow */
467 i = i + j;
468 }
469 } else {
470 /* negative number */
471 s++;
472 while (--len > 0) {
473 j = (*s++) - '0';
474 if (j > 9) return 1; /* invalid char */
475 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
476 i = i * 10;
477 if (i - j > i) return 1; /* check for subtract overflow */
478 i = i - j;
479 }
480 }
481 *ret = i;
482 return 0;
483}
484
485
486/* This function reads exactly <len> chars from <s> and converts them to a
487 * signed integer which it stores into <ret>. It accurately detects any error
488 * (truncated string, invalid chars, overflows). It is meant to be used in
489 * applications designed for hostile environments. It returns zero when the
490 * number has successfully been converted, non-zero otherwise. When an error
491 * is returned, the <ret> value is left untouched. It is about 3 times slower
492 * than str2irc().
493 */
494#ifndef LLONG_MAX
495#define LLONG_MAX 9223372036854775807LL
496#define LLONG_MIN (-LLONG_MAX - 1LL)
497#endif
498
499int strl2llrc(const char *s, int len, long long *ret)
500{
501 long long i = 0;
502 int j;
503
504 if (!len)
505 return 1;
506
507 if (*s != '-') {
508 /* positive number */
509 while (len-- > 0) {
510 j = (*s++) - '0';
511 if (j > 9) return 1; /* invalid char */
512 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
513 i = i * 10LL;
514 if (i + j < i) return 1; /* check for addition overflow */
515 i = i + j;
516 }
517 } else {
518 /* negative number */
519 s++;
520 while (--len > 0) {
521 j = (*s++) - '0';
522 if (j > 9) return 1; /* invalid char */
523 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
524 i = i * 10LL;
525 if (i - j > i) return 1; /* check for subtract overflow */
526 i = i - j;
527 }
528 }
529 *ret = i;
530 return 0;
531}
532
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100533/* This function parses a time value optionally followed by a unit suffix among
534 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
535 * expected by the caller. The computation does its best to avoid overflows.
536 * The value is returned in <ret> if everything is fine, and a NULL is returned
537 * by the function. In case of error, a pointer to the error is returned and
538 * <ret> is left untouched. Values are automatically rounded up when needed.
539 */
540const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
541{
542 unsigned imult, idiv;
543 unsigned omult, odiv;
544 unsigned value;
545
546 omult = odiv = 1;
547
548 switch (unit_flags & TIME_UNIT_MASK) {
549 case TIME_UNIT_US: omult = 1000000; break;
550 case TIME_UNIT_MS: omult = 1000; break;
551 case TIME_UNIT_S: break;
552 case TIME_UNIT_MIN: odiv = 60; break;
553 case TIME_UNIT_HOUR: odiv = 3600; break;
554 case TIME_UNIT_DAY: odiv = 86400; break;
555 default: break;
556 }
557
558 value = 0;
559
560 while (1) {
561 unsigned int j;
562
563 j = *text - '0';
564 if (j > 9)
565 break;
566 text++;
567 value *= 10;
568 value += j;
569 }
570
571 imult = idiv = 1;
572 switch (*text) {
573 case '\0': /* no unit = default unit */
574 imult = omult = idiv = odiv = 1;
575 break;
576 case 's': /* second = unscaled unit */
577 break;
578 case 'u': /* microsecond : "us" */
579 if (text[1] == 's') {
580 idiv = 1000000;
581 text++;
582 }
583 break;
584 case 'm': /* millisecond : "ms" or minute: "m" */
585 if (text[1] == 's') {
586 idiv = 1000;
587 text++;
588 } else
589 imult = 60;
590 break;
591 case 'h': /* hour : "h" */
592 imult = 3600;
593 break;
594 case 'd': /* day : "d" */
595 imult = 86400;
596 break;
597 default:
598 return text;
599 break;
600 }
601
602 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
603 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
604 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
605 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
606
607 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
608 *ret = value;
609 return NULL;
610}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100611
Willy Tarreaubaaee002006-06-26 02:48:02 +0200612/*
613 * Local variables:
614 * c-indent-level: 8
615 * c-basic-offset: 8
616 * End:
617 */