blob: 93cf1f89e5d9a5eb4bfbab87a0d6cb2e333cd817 [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 */
86struct sockaddr_un *str2sun(char *str)
87{
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 str = strdup(str);
93 if (str == NULL)
94 goto out_nofree;
95
96 strsz = strlen(str) + 1;
Willy Tarreau127f9662007-12-06 00:53:51 +010097 if (strsz > sizeof(su.sun_path)) {
Robert Tsai81ae1952007-12-05 10:47:29 +010098 Alert("Socket path '%s' too long (max %d)\n",
Willy Tarreau127f9662007-12-06 00:53:51 +010099 str, sizeof(su.sun_path) - 1);
Robert Tsai81ae1952007-12-05 10:47:29 +0100100 goto out_nofree;
101 }
Willy Tarreau127f9662007-12-06 00:53:51 +0100102 su.sun_family = AF_UNIX;
103 memcpy(su.sun_path, str, strsz);
Robert Tsai81ae1952007-12-05 10:47:29 +0100104
105 free(str);
106 out_nofree:
Willy Tarreau127f9662007-12-06 00:53:51 +0100107 return &su;
Robert Tsai81ae1952007-12-05 10:47:29 +0100108}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200109
110/*
111 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
112 *
113 * It looks like this one would be a good candidate for inlining, but this is
114 * not interesting because it around 35 bytes long and often called multiple
115 * times within the same function.
116 */
117int ishex(char s)
118{
119 s -= '0';
120 if ((unsigned char)s <= 9)
121 return 1;
122 s -= 'A' - '0';
123 if ((unsigned char)s <= 5)
124 return 1;
125 s -= 'a' - 'A';
126 if ((unsigned char)s <= 5)
127 return 1;
128 return 0;
129}
130
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100131/*
132 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
133 * invalid character is found, a pointer to it is returned. If everything is
134 * fine, NULL is returned.
135 */
136const char *invalid_char(const char *name)
137{
138 if (!*name)
139 return name;
140
141 while (*name) {
Willy Tarreau127f9662007-12-06 00:53:51 +0100142 if (!isalnum((int)*name) && *name != '.' && *name != ':' &&
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100143 *name != '_' && *name != '-')
144 return name;
145 name++;
146 }
147 return NULL;
148}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149
150/*
151 * converts <str> to a struct sockaddr_in* which is locally allocated.
152 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
153 * a host name, or empty or "*" to indicate INADDR_ANY.
154 */
155struct sockaddr_in *str2sa(char *str)
156{
157 static struct sockaddr_in sa;
158 char *c;
159 int port;
160
161 memset(&sa, 0, sizeof(sa));
162 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200163 if (str == NULL)
164 goto out_nofree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200165
166 if ((c = strrchr(str,':')) != NULL) {
167 *c++ = '\0';
168 port = atol(c);
169 }
170 else
171 port = 0;
172
173 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
174 sa.sin_addr.s_addr = INADDR_ANY;
175 }
176 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
177 struct hostent *he;
178
179 if ((he = gethostbyname(str)) == NULL) {
180 Alert("Invalid server name: '%s'\n", str);
181 }
182 else
183 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
184 }
185 sa.sin_port = htons(port);
186 sa.sin_family = AF_INET;
187
188 free(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200189 out_nofree:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190 return &sa;
191}
192
193/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200194 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200195 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
196 * is optionnal and either in the dotted or CIDR notation.
197 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
198 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200199int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200200{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200201 __label__ out_free, out_err;
202 char *c, *s;
203 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200204 unsigned long len;
205
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200206 s = strdup(str);
207 if (!s)
208 return 0;
209
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210 memset(mask, 0, sizeof(*mask));
211 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200212
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200213 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200214 *c++ = '\0';
215 /* c points to the mask */
216 if (strchr(c, '.') != NULL) { /* dotted notation */
217 if (!inet_pton(AF_INET, c, mask))
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200218 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200219 }
220 else { /* mask length */
221 char *err;
222 len = strtol(c, &err, 10);
223 if (!*c || (err && *err) || (unsigned)len > 32)
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200224 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200225 if (len)
226 mask->s_addr = htonl(~0UL << (32 - len));
227 else
228 mask->s_addr = 0;
229 }
230 }
231 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100232 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200234 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200235 struct hostent *he;
236
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200237 if ((he = gethostbyname(s)) == NULL) {
238 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200239 }
240 else
241 *addr = *(struct in_addr *) *(he->h_addr_list);
242 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200243
244 ret_val = 1;
245 out_free:
246 free(s);
247 return ret_val;
248 out_err:
249 ret_val = 0;
250 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251}
252
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100253
254/*
255 * Parse IP address found in url.
256 */
257static int url2ip(const char *addr, struct in_addr *dst)
258{
259 int saw_digit, octets, ch;
260 u_char tmp[4], *tp;
261 const char *cp = addr;
262
263 saw_digit = 0;
264 octets = 0;
265 *(tp = tmp) = 0;
266
267 while (*addr) {
268 unsigned char digit = (ch = *addr++) - '0';
269 if (digit > 9 && ch != '.')
270 break;
271 if (digit <= 9) {
272 u_int new = *tp * 10 + digit;
273 if (new > 255)
274 return 0;
275 *tp = new;
276 if (!saw_digit) {
277 if (++octets > 4)
278 return 0;
279 saw_digit = 1;
280 }
281 } else if (ch == '.' && saw_digit) {
282 if (octets == 4)
283 return 0;
284 *++tp = 0;
285 saw_digit = 0;
286 } else
287 return 0;
288 }
289
290 if (octets < 4)
291 return 0;
292
293 memcpy(&dst->s_addr, tmp, 4);
294 return addr-cp-1;
295}
296
297/*
298 * Resolve destination server from URL. Convert <str> to a sockaddr_in*.
299 */
300int url2sa(const char *url, int ulen, struct sockaddr_in *addr)
301{
302 const char *curr = url, *cp = url;
303 int ret, url_code = 0;
304 unsigned int http_code = 0;
305
306 /* Cleanup the room */
307 addr->sin_family = AF_INET;
308 addr->sin_addr.s_addr = 0;
309 addr->sin_port = 0;
310
311 /* Firstly, try to find :// pattern */
312 while (curr < url+ulen && url_code != 0x3a2f2f) {
313 url_code = ((url_code & 0xffff) << 8);
314 url_code += (unsigned char)*curr++;
315 }
316
317 /* Secondly, if :// pattern is found, verify parsed stuff
318 * before pattern is matching our http pattern.
319 * If so parse ip address and port in uri.
320 *
321 * WARNING: Current code doesn't support dynamic async dns resolver.
322 */
323 if (url_code == 0x3a2f2f) {
324 while (cp < curr - 3)
325 http_code = (http_code << 8) + *cp++;
326 http_code |= 0x20202020; /* Turn everything to lower case */
327
328 /* HTTP url matching */
329 if (http_code == 0x68747470) {
330 /* We are looking for IP address. If you want to parse and
331 * resolve hostname found in url, you can use str2sa(), but
332 * be warned this can slow down global daemon performances
333 * while handling lagging dns responses.
334 */
335 ret = url2ip(curr, &addr->sin_addr);
336 if (!ret)
337 return -1;
338 curr += ret;
Willy Tarreaud1cd2762007-12-02 10:55:56 +0100339 addr->sin_port = (*curr == ':') ? str2uic(++curr) : 80;
340 addr->sin_port = htons(addr->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100341 }
342 return 0;
343 }
344
345 return -1;
346}
347
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348/* will try to encode the string <string> replacing all characters tagged in
349 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
350 * prefixed by <escape>, and will store the result between <start> (included)
351 * and <stop> (excluded), and will always terminate the string with a '\0'
352 * before <stop>. The position of the '\0' is returned if the conversion
353 * completes. If bytes are missing between <start> and <stop>, then the
354 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
355 * cannot even be stored so we return <start> without writing the 0.
356 * The input string must also be zero-terminated.
357 */
358const char hextab[16] = "0123456789ABCDEF";
359char *encode_string(char *start, char *stop,
360 const char escape, const fd_set *map,
361 const char *string)
362{
363 if (start < stop) {
364 stop--; /* reserve one byte for the final '\0' */
365 while (start < stop && *string != '\0') {
366 if (!FD_ISSET((unsigned char)(*string), map))
367 *start++ = *string;
368 else {
369 if (start + 3 >= stop)
370 break;
371 *start++ = escape;
372 *start++ = hextab[(*string >> 4) & 15];
373 *start++ = hextab[*string & 15];
374 }
375 string++;
376 }
377 *start = '\0';
378 }
379 return start;
380}
381
382
Willy Tarreau6911fa42007-03-04 18:06:08 +0100383unsigned int str2ui(const char *s)
384{
385 return __str2ui(s);
386}
387
388unsigned int str2uic(const char *s)
389{
390 return __str2uic(s);
391}
392
393unsigned int strl2ui(const char *s, int len)
394{
395 return __strl2ui(s, len);
396}
397
398unsigned int strl2uic(const char *s, int len)
399{
400 return __strl2uic(s, len);
401}
402
403/* This one is 7 times faster than strtol() on athlon with checks.
404 * It returns the value of the number composed of all valid digits read,
405 * and can process negative numbers too.
406 */
407int strl2ic(const char *s, int len)
408{
409 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200410 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100411
412 if (len > 0) {
413 if (*s != '-') {
414 /* positive number */
415 while (len-- > 0) {
416 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200417 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100418 if (j > 9)
419 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200420 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100421 }
422 } else {
423 /* negative number */
424 s++;
425 while (--len > 0) {
426 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200427 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100428 if (j > 9)
429 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200430 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100431 }
432 }
433 }
434 return i;
435}
436
437
438/* This function reads exactly <len> chars from <s> and converts them to a
439 * signed integer which it stores into <ret>. It accurately detects any error
440 * (truncated string, invalid chars, overflows). It is meant to be used in
441 * applications designed for hostile environments. It returns zero when the
442 * number has successfully been converted, non-zero otherwise. When an error
443 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
444 * faster than strtol().
445 */
446int strl2irc(const char *s, int len, int *ret)
447{
448 int i = 0;
449 int j;
450
451 if (!len)
452 return 1;
453
454 if (*s != '-') {
455 /* positive number */
456 while (len-- > 0) {
457 j = (*s++) - '0';
458 if (j > 9) return 1; /* invalid char */
459 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
460 i = i * 10;
461 if (i + j < i) return 1; /* check for addition overflow */
462 i = i + j;
463 }
464 } else {
465 /* negative number */
466 s++;
467 while (--len > 0) {
468 j = (*s++) - '0';
469 if (j > 9) return 1; /* invalid char */
470 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
471 i = i * 10;
472 if (i - j > i) return 1; /* check for subtract overflow */
473 i = i - j;
474 }
475 }
476 *ret = i;
477 return 0;
478}
479
480
481/* This function reads exactly <len> chars from <s> and converts them to a
482 * signed integer which it stores into <ret>. It accurately detects any error
483 * (truncated string, invalid chars, overflows). It is meant to be used in
484 * applications designed for hostile environments. It returns zero when the
485 * number has successfully been converted, non-zero otherwise. When an error
486 * is returned, the <ret> value is left untouched. It is about 3 times slower
487 * than str2irc().
488 */
489#ifndef LLONG_MAX
490#define LLONG_MAX 9223372036854775807LL
491#define LLONG_MIN (-LLONG_MAX - 1LL)
492#endif
493
494int strl2llrc(const char *s, int len, long long *ret)
495{
496 long long i = 0;
497 int j;
498
499 if (!len)
500 return 1;
501
502 if (*s != '-') {
503 /* positive number */
504 while (len-- > 0) {
505 j = (*s++) - '0';
506 if (j > 9) return 1; /* invalid char */
507 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
508 i = i * 10LL;
509 if (i + j < i) return 1; /* check for addition overflow */
510 i = i + j;
511 }
512 } else {
513 /* negative number */
514 s++;
515 while (--len > 0) {
516 j = (*s++) - '0';
517 if (j > 9) return 1; /* invalid char */
518 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
519 i = i * 10LL;
520 if (i - j > i) return 1; /* check for subtract overflow */
521 i = i - j;
522 }
523 }
524 *ret = i;
525 return 0;
526}
527
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100528/* This function parses a time value optionally followed by a unit suffix among
529 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
530 * expected by the caller. The computation does its best to avoid overflows.
531 * The value is returned in <ret> if everything is fine, and a NULL is returned
532 * by the function. In case of error, a pointer to the error is returned and
533 * <ret> is left untouched. Values are automatically rounded up when needed.
534 */
535const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
536{
537 unsigned imult, idiv;
538 unsigned omult, odiv;
539 unsigned value;
540
541 omult = odiv = 1;
542
543 switch (unit_flags & TIME_UNIT_MASK) {
544 case TIME_UNIT_US: omult = 1000000; break;
545 case TIME_UNIT_MS: omult = 1000; break;
546 case TIME_UNIT_S: break;
547 case TIME_UNIT_MIN: odiv = 60; break;
548 case TIME_UNIT_HOUR: odiv = 3600; break;
549 case TIME_UNIT_DAY: odiv = 86400; break;
550 default: break;
551 }
552
553 value = 0;
554
555 while (1) {
556 unsigned int j;
557
558 j = *text - '0';
559 if (j > 9)
560 break;
561 text++;
562 value *= 10;
563 value += j;
564 }
565
566 imult = idiv = 1;
567 switch (*text) {
568 case '\0': /* no unit = default unit */
569 imult = omult = idiv = odiv = 1;
570 break;
571 case 's': /* second = unscaled unit */
572 break;
573 case 'u': /* microsecond : "us" */
574 if (text[1] == 's') {
575 idiv = 1000000;
576 text++;
577 }
578 break;
579 case 'm': /* millisecond : "ms" or minute: "m" */
580 if (text[1] == 's') {
581 idiv = 1000;
582 text++;
583 } else
584 imult = 60;
585 break;
586 case 'h': /* hour : "h" */
587 imult = 3600;
588 break;
589 case 'd': /* day : "d" */
590 imult = 86400;
591 break;
592 default:
593 return text;
594 break;
595 }
596
597 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
598 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
599 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
600 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
601
602 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
603 *ret = value;
604 return NULL;
605}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100606
Willy Tarreaubaaee002006-06-26 02:48:02 +0200607/*
608 * Local variables:
609 * c-indent-level: 8
610 * c-basic-offset: 8
611 * End:
612 */