blob: 7f749f957505a0517f9100250f80e2e644588c9f [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/*
144 * converts <str> to a struct sockaddr_in* which is locally allocated.
145 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
146 * a host name, or empty or "*" to indicate INADDR_ANY.
147 */
148struct sockaddr_in *str2sa(char *str)
149{
150 static struct sockaddr_in sa;
151 char *c;
152 int port;
153
154 memset(&sa, 0, sizeof(sa));
155 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200156 if (str == NULL)
157 goto out_nofree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158
159 if ((c = strrchr(str,':')) != NULL) {
160 *c++ = '\0';
161 port = atol(c);
162 }
163 else
164 port = 0;
165
166 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
167 sa.sin_addr.s_addr = INADDR_ANY;
168 }
169 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
170 struct hostent *he;
171
172 if ((he = gethostbyname(str)) == NULL) {
173 Alert("Invalid server name: '%s'\n", str);
174 }
175 else
176 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
177 }
178 sa.sin_port = htons(port);
179 sa.sin_family = AF_INET;
180
181 free(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200182 out_nofree:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183 return &sa;
184}
185
186/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200187 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200188 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
189 * is optionnal and either in the dotted or CIDR notation.
190 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
191 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200192int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200193{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200194 __label__ out_free, out_err;
195 char *c, *s;
196 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200197 unsigned long len;
198
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200199 s = strdup(str);
200 if (!s)
201 return 0;
202
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203 memset(mask, 0, sizeof(*mask));
204 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200205
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200206 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200207 *c++ = '\0';
208 /* c points to the mask */
209 if (strchr(c, '.') != NULL) { /* dotted notation */
210 if (!inet_pton(AF_INET, c, mask))
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200211 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212 }
213 else { /* mask length */
214 char *err;
215 len = strtol(c, &err, 10);
216 if (!*c || (err && *err) || (unsigned)len > 32)
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200217 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200218 if (len)
219 mask->s_addr = htonl(~0UL << (32 - len));
220 else
221 mask->s_addr = 0;
222 }
223 }
224 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100225 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200226 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200227 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200228 struct hostent *he;
229
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200230 if ((he = gethostbyname(s)) == NULL) {
231 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200232 }
233 else
234 *addr = *(struct in_addr *) *(he->h_addr_list);
235 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200236
237 ret_val = 1;
238 out_free:
239 free(s);
240 return ret_val;
241 out_err:
242 ret_val = 0;
243 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200244}
245
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100246
247/*
248 * Parse IP address found in url.
249 */
250static int url2ip(const char *addr, struct in_addr *dst)
251{
252 int saw_digit, octets, ch;
253 u_char tmp[4], *tp;
254 const char *cp = addr;
255
256 saw_digit = 0;
257 octets = 0;
258 *(tp = tmp) = 0;
259
260 while (*addr) {
261 unsigned char digit = (ch = *addr++) - '0';
262 if (digit > 9 && ch != '.')
263 break;
264 if (digit <= 9) {
265 u_int new = *tp * 10 + digit;
266 if (new > 255)
267 return 0;
268 *tp = new;
269 if (!saw_digit) {
270 if (++octets > 4)
271 return 0;
272 saw_digit = 1;
273 }
274 } else if (ch == '.' && saw_digit) {
275 if (octets == 4)
276 return 0;
277 *++tp = 0;
278 saw_digit = 0;
279 } else
280 return 0;
281 }
282
283 if (octets < 4)
284 return 0;
285
286 memcpy(&dst->s_addr, tmp, 4);
287 return addr-cp-1;
288}
289
290/*
291 * Resolve destination server from URL. Convert <str> to a sockaddr_in*.
292 */
293int url2sa(const char *url, int ulen, struct sockaddr_in *addr)
294{
295 const char *curr = url, *cp = url;
296 int ret, url_code = 0;
297 unsigned int http_code = 0;
298
299 /* Cleanup the room */
300 addr->sin_family = AF_INET;
301 addr->sin_addr.s_addr = 0;
302 addr->sin_port = 0;
303
304 /* Firstly, try to find :// pattern */
305 while (curr < url+ulen && url_code != 0x3a2f2f) {
306 url_code = ((url_code & 0xffff) << 8);
307 url_code += (unsigned char)*curr++;
308 }
309
310 /* Secondly, if :// pattern is found, verify parsed stuff
311 * before pattern is matching our http pattern.
312 * If so parse ip address and port in uri.
313 *
314 * WARNING: Current code doesn't support dynamic async dns resolver.
315 */
316 if (url_code == 0x3a2f2f) {
317 while (cp < curr - 3)
318 http_code = (http_code << 8) + *cp++;
319 http_code |= 0x20202020; /* Turn everything to lower case */
320
321 /* HTTP url matching */
322 if (http_code == 0x68747470) {
323 /* We are looking for IP address. If you want to parse and
324 * resolve hostname found in url, you can use str2sa(), but
325 * be warned this can slow down global daemon performances
326 * while handling lagging dns responses.
327 */
328 ret = url2ip(curr, &addr->sin_addr);
329 if (!ret)
330 return -1;
331 curr += ret;
Willy Tarreaud1cd2762007-12-02 10:55:56 +0100332 addr->sin_port = (*curr == ':') ? str2uic(++curr) : 80;
333 addr->sin_port = htons(addr->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100334 }
335 return 0;
336 }
337
338 return -1;
339}
340
Willy Tarreaubaaee002006-06-26 02:48:02 +0200341/* will try to encode the string <string> replacing all characters tagged in
342 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
343 * prefixed by <escape>, and will store the result between <start> (included)
344 * and <stop> (excluded), and will always terminate the string with a '\0'
345 * before <stop>. The position of the '\0' is returned if the conversion
346 * completes. If bytes are missing between <start> and <stop>, then the
347 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
348 * cannot even be stored so we return <start> without writing the 0.
349 * The input string must also be zero-terminated.
350 */
351const char hextab[16] = "0123456789ABCDEF";
352char *encode_string(char *start, char *stop,
353 const char escape, const fd_set *map,
354 const char *string)
355{
356 if (start < stop) {
357 stop--; /* reserve one byte for the final '\0' */
358 while (start < stop && *string != '\0') {
359 if (!FD_ISSET((unsigned char)(*string), map))
360 *start++ = *string;
361 else {
362 if (start + 3 >= stop)
363 break;
364 *start++ = escape;
365 *start++ = hextab[(*string >> 4) & 15];
366 *start++ = hextab[*string & 15];
367 }
368 string++;
369 }
370 *start = '\0';
371 }
372 return start;
373}
374
375
Willy Tarreau6911fa42007-03-04 18:06:08 +0100376unsigned int str2ui(const char *s)
377{
378 return __str2ui(s);
379}
380
381unsigned int str2uic(const char *s)
382{
383 return __str2uic(s);
384}
385
386unsigned int strl2ui(const char *s, int len)
387{
388 return __strl2ui(s, len);
389}
390
391unsigned int strl2uic(const char *s, int len)
392{
393 return __strl2uic(s, len);
394}
395
396/* This one is 7 times faster than strtol() on athlon with checks.
397 * It returns the value of the number composed of all valid digits read,
398 * and can process negative numbers too.
399 */
400int strl2ic(const char *s, int len)
401{
402 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200403 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100404
405 if (len > 0) {
406 if (*s != '-') {
407 /* positive number */
408 while (len-- > 0) {
409 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200410 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100411 if (j > 9)
412 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200413 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100414 }
415 } else {
416 /* negative number */
417 s++;
418 while (--len > 0) {
419 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200420 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100421 if (j > 9)
422 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200423 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100424 }
425 }
426 }
427 return i;
428}
429
430
431/* This function reads exactly <len> chars from <s> and converts them to a
432 * signed integer which it stores into <ret>. It accurately detects any error
433 * (truncated string, invalid chars, overflows). It is meant to be used in
434 * applications designed for hostile environments. It returns zero when the
435 * number has successfully been converted, non-zero otherwise. When an error
436 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
437 * faster than strtol().
438 */
439int strl2irc(const char *s, int len, int *ret)
440{
441 int i = 0;
442 int j;
443
444 if (!len)
445 return 1;
446
447 if (*s != '-') {
448 /* positive number */
449 while (len-- > 0) {
450 j = (*s++) - '0';
451 if (j > 9) return 1; /* invalid char */
452 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
453 i = i * 10;
454 if (i + j < i) return 1; /* check for addition overflow */
455 i = i + j;
456 }
457 } else {
458 /* negative number */
459 s++;
460 while (--len > 0) {
461 j = (*s++) - '0';
462 if (j > 9) return 1; /* invalid char */
463 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
464 i = i * 10;
465 if (i - j > i) return 1; /* check for subtract overflow */
466 i = i - j;
467 }
468 }
469 *ret = i;
470 return 0;
471}
472
473
474/* This function reads exactly <len> chars from <s> and converts them to a
475 * signed integer which it stores into <ret>. It accurately detects any error
476 * (truncated string, invalid chars, overflows). It is meant to be used in
477 * applications designed for hostile environments. It returns zero when the
478 * number has successfully been converted, non-zero otherwise. When an error
479 * is returned, the <ret> value is left untouched. It is about 3 times slower
480 * than str2irc().
481 */
482#ifndef LLONG_MAX
483#define LLONG_MAX 9223372036854775807LL
484#define LLONG_MIN (-LLONG_MAX - 1LL)
485#endif
486
487int strl2llrc(const char *s, int len, long long *ret)
488{
489 long long i = 0;
490 int j;
491
492 if (!len)
493 return 1;
494
495 if (*s != '-') {
496 /* positive number */
497 while (len-- > 0) {
498 j = (*s++) - '0';
499 if (j > 9) return 1; /* invalid char */
500 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
501 i = i * 10LL;
502 if (i + j < i) return 1; /* check for addition overflow */
503 i = i + j;
504 }
505 } else {
506 /* negative number */
507 s++;
508 while (--len > 0) {
509 j = (*s++) - '0';
510 if (j > 9) return 1; /* invalid char */
511 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
512 i = i * 10LL;
513 if (i - j > i) return 1; /* check for subtract overflow */
514 i = i - j;
515 }
516 }
517 *ret = i;
518 return 0;
519}
520
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100521/* This function parses a time value optionally followed by a unit suffix among
522 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
523 * expected by the caller. The computation does its best to avoid overflows.
524 * The value is returned in <ret> if everything is fine, and a NULL is returned
525 * by the function. In case of error, a pointer to the error is returned and
526 * <ret> is left untouched. Values are automatically rounded up when needed.
527 */
528const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
529{
530 unsigned imult, idiv;
531 unsigned omult, odiv;
532 unsigned value;
533
534 omult = odiv = 1;
535
536 switch (unit_flags & TIME_UNIT_MASK) {
537 case TIME_UNIT_US: omult = 1000000; break;
538 case TIME_UNIT_MS: omult = 1000; break;
539 case TIME_UNIT_S: break;
540 case TIME_UNIT_MIN: odiv = 60; break;
541 case TIME_UNIT_HOUR: odiv = 3600; break;
542 case TIME_UNIT_DAY: odiv = 86400; break;
543 default: break;
544 }
545
546 value = 0;
547
548 while (1) {
549 unsigned int j;
550
551 j = *text - '0';
552 if (j > 9)
553 break;
554 text++;
555 value *= 10;
556 value += j;
557 }
558
559 imult = idiv = 1;
560 switch (*text) {
561 case '\0': /* no unit = default unit */
562 imult = omult = idiv = odiv = 1;
563 break;
564 case 's': /* second = unscaled unit */
565 break;
566 case 'u': /* microsecond : "us" */
567 if (text[1] == 's') {
568 idiv = 1000000;
569 text++;
570 }
571 break;
572 case 'm': /* millisecond : "ms" or minute: "m" */
573 if (text[1] == 's') {
574 idiv = 1000;
575 text++;
576 } else
577 imult = 60;
578 break;
579 case 'h': /* hour : "h" */
580 imult = 3600;
581 break;
582 case 'd': /* day : "d" */
583 imult = 86400;
584 break;
585 default:
586 return text;
587 break;
588 }
589
590 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
591 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
592 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
593 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
594
595 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
596 *ret = value;
597 return NULL;
598}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100599
Willy Tarreaubaaee002006-06-26 02:48:02 +0200600/*
601 * Local variables:
602 * c-indent-level: 8
603 * c-basic-offset: 8
604 * End:
605 */