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