blob: 906f2a730f5ccd0457324eae7a9c587e8436d09b [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
Willy Tarreaue7239b52009-03-29 13:41:58 +020029 *
30 * The HTML version needs room for adding the 25 characters
31 * '<span class="rls"></span>' around digits at positions 3N+1 in order
32 * to add spacing at up to 6 positions : 18 446 744 073 709 551 615
Willy Tarreau72d759c2007-10-25 12:14:10 +020033 */
Willy Tarreaue7239b52009-03-29 13:41:58 +020034char itoa_str[10][171];
Willy Tarreaubaaee002006-06-26 02:48:02 +020035
36/*
37 * copies at most <size-1> chars from <src> to <dst>. Last char is always
38 * set to 0, unless <size> is 0. The number of chars copied is returned
39 * (excluding the terminating zero).
40 * This code has been optimized for size and speed : on x86, it's 45 bytes
41 * long, uses only registers, and consumes only 4 cycles per char.
42 */
43int strlcpy2(char *dst, const char *src, int size)
44{
45 char *orig = dst;
46 if (size) {
47 while (--size && (*dst = *src)) {
48 src++; dst++;
49 }
50 *dst = 0;
51 }
52 return dst - orig;
53}
54
55/*
Willy Tarreau72d759c2007-10-25 12:14:10 +020056 * This function simply returns a locally allocated string containing
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 * the ascii representation for number 'n' in decimal.
58 */
Willy Tarreau72d759c2007-10-25 12:14:10 +020059const char *ultoa_r(unsigned long n, char *buffer, int size)
Willy Tarreaubaaee002006-06-26 02:48:02 +020060{
61 char *pos;
62
Willy Tarreau72d759c2007-10-25 12:14:10 +020063 pos = buffer + size - 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020064 *pos-- = '\0';
65
66 do {
67 *pos-- = '0' + n % 10;
68 n /= 10;
Willy Tarreau72d759c2007-10-25 12:14:10 +020069 } while (n && pos >= buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +020070 return pos + 1;
71}
72
Willy Tarreau91092e52007-10-25 16:58:42 +020073/*
Willy Tarreaue7239b52009-03-29 13:41:58 +020074 * This function simply returns a locally allocated string containing
75 * the ascii representation for number 'n' in decimal, formatted for
76 * HTML output with tags to create visual grouping by 3 digits. The
77 * output needs to support at least 171 characters.
78 */
79const char *ulltoh_r(unsigned long long n, char *buffer, int size)
80{
81 char *start;
82 int digit = 0;
83
84 start = buffer + size;
85 *--start = '\0';
86
87 do {
88 if (digit == 3 && start >= buffer + 7)
89 memcpy(start -= 7, "</span>", 7);
90
91 if (start >= buffer + 1) {
92 *--start = '0' + n % 10;
93 n /= 10;
94 }
95
96 if (digit == 3 && start >= buffer + 18)
97 memcpy(start -= 18, "<span class=\"rls\">", 18);
98
99 if (digit++ == 3)
100 digit = 1;
101 } while (n && start > buffer);
102 return start;
103}
104
105/*
Willy Tarreau91092e52007-10-25 16:58:42 +0200106 * This function simply returns a locally allocated string containing the ascii
107 * representation for number 'n' in decimal, unless n is 0 in which case it
108 * returns the alternate string (or an empty string if the alternate string is
109 * NULL). It use is intended for limits reported in reports, where it's
110 * desirable not to display anything if there is no limit. Warning! it shares
111 * the same vector as ultoa_r().
112 */
113const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
114{
115 return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
116}
117
Robert Tsai81ae1952007-12-05 10:47:29 +0100118/*
119 * converts <str> to a struct sockaddr_un* which is locally allocated.
120 * The format is "/path", where "/path" is a path to a UNIX domain socket.
121 */
Willy Tarreaucaf720d2008-03-07 10:07:04 +0100122struct sockaddr_un *str2sun(const char *str)
Robert Tsai81ae1952007-12-05 10:47:29 +0100123{
Willy Tarreau127f9662007-12-06 00:53:51 +0100124 static struct sockaddr_un su;
Robert Tsai81ae1952007-12-05 10:47:29 +0100125 int strsz; /* length included null */
126
Willy Tarreau127f9662007-12-06 00:53:51 +0100127 memset(&su, 0, sizeof(su));
Robert Tsai81ae1952007-12-05 10:47:29 +0100128 strsz = strlen(str) + 1;
Willy Tarreau127f9662007-12-06 00:53:51 +0100129 if (strsz > sizeof(su.sun_path)) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100130 Alert("Socket path '%s' too long (max %d)\n",
Willy Tarreau5e4a6f12009-04-11 19:42:49 +0200131 str, (int)sizeof(su.sun_path) - 1);
Willy Tarreaucaf720d2008-03-07 10:07:04 +0100132 } else {
133 su.sun_family = AF_UNIX;
134 memcpy(su.sun_path, str, strsz);
Robert Tsai81ae1952007-12-05 10:47:29 +0100135 }
Willy Tarreau127f9662007-12-06 00:53:51 +0100136 return &su;
Robert Tsai81ae1952007-12-05 10:47:29 +0100137}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200138
139/*
140 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
141 *
142 * It looks like this one would be a good candidate for inlining, but this is
143 * not interesting because it around 35 bytes long and often called multiple
144 * times within the same function.
145 */
146int ishex(char s)
147{
148 s -= '0';
149 if ((unsigned char)s <= 9)
150 return 1;
151 s -= 'A' - '0';
152 if ((unsigned char)s <= 5)
153 return 1;
154 s -= 'a' - 'A';
155 if ((unsigned char)s <= 5)
156 return 1;
157 return 0;
158}
159
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100160/*
161 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
162 * invalid character is found, a pointer to it is returned. If everything is
163 * fine, NULL is returned.
164 */
165const char *invalid_char(const char *name)
166{
167 if (!*name)
168 return name;
169
170 while (*name) {
Willy Tarreau127f9662007-12-06 00:53:51 +0100171 if (!isalnum((int)*name) && *name != '.' && *name != ':' &&
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100172 *name != '_' && *name != '-')
173 return name;
174 name++;
175 }
176 return NULL;
177}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178
179/*
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200180 * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-].
181 * If an invalid character is found, a pointer to it is returned.
182 * If everything is fine, NULL is returned.
183 */
184const char *invalid_domainchar(const char *name) {
185
186 if (!*name)
187 return name;
188
189 while (*name) {
190 if (!isalnum((int)*name) && *name != '.' &&
191 *name != '_' && *name != '-')
192 return name;
193
194 name++;
195 }
196
197 return NULL;
198}
199
200/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200201 * converts <str> to a struct sockaddr_in* which is locally allocated.
202 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
203 * a host name, or empty or "*" to indicate INADDR_ANY.
204 */
205struct sockaddr_in *str2sa(char *str)
206{
207 static struct sockaddr_in sa;
208 char *c;
209 int port;
210
211 memset(&sa, 0, sizeof(sa));
212 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200213 if (str == NULL)
214 goto out_nofree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200215
216 if ((c = strrchr(str,':')) != NULL) {
217 *c++ = '\0';
218 port = atol(c);
219 }
220 else
221 port = 0;
222
223 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
224 sa.sin_addr.s_addr = INADDR_ANY;
225 }
226 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
227 struct hostent *he;
228
229 if ((he = gethostbyname(str)) == NULL) {
230 Alert("Invalid server name: '%s'\n", str);
231 }
232 else
233 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
234 }
235 sa.sin_port = htons(port);
236 sa.sin_family = AF_INET;
237
238 free(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200239 out_nofree:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200240 return &sa;
241}
242
243/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200244 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
246 * is optionnal and either in the dotted or CIDR notation.
247 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
248 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200249int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200250{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200251 __label__ out_free, out_err;
252 char *c, *s;
253 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200254 unsigned long len;
255
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200256 s = strdup(str);
257 if (!s)
258 return 0;
259
Willy Tarreaubaaee002006-06-26 02:48:02 +0200260 memset(mask, 0, sizeof(*mask));
261 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200262
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200263 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200264 *c++ = '\0';
265 /* c points to the mask */
266 if (strchr(c, '.') != NULL) { /* dotted notation */
267 if (!inet_pton(AF_INET, c, mask))
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200268 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200269 }
270 else { /* mask length */
271 char *err;
272 len = strtol(c, &err, 10);
273 if (!*c || (err && *err) || (unsigned)len > 32)
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200274 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200275 if (len)
276 mask->s_addr = htonl(~0UL << (32 - len));
277 else
278 mask->s_addr = 0;
279 }
280 }
281 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100282 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200283 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200284 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200285 struct hostent *he;
286
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200287 if ((he = gethostbyname(s)) == NULL) {
288 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200289 }
290 else
291 *addr = *(struct in_addr *) *(he->h_addr_list);
292 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200293
294 ret_val = 1;
295 out_free:
296 free(s);
297 return ret_val;
298 out_err:
299 ret_val = 0;
300 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200301}
302
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100303
304/*
305 * Parse IP address found in url.
306 */
307static int url2ip(const char *addr, struct in_addr *dst)
308{
309 int saw_digit, octets, ch;
310 u_char tmp[4], *tp;
311 const char *cp = addr;
312
313 saw_digit = 0;
314 octets = 0;
315 *(tp = tmp) = 0;
316
317 while (*addr) {
318 unsigned char digit = (ch = *addr++) - '0';
319 if (digit > 9 && ch != '.')
320 break;
321 if (digit <= 9) {
322 u_int new = *tp * 10 + digit;
323 if (new > 255)
324 return 0;
325 *tp = new;
326 if (!saw_digit) {
327 if (++octets > 4)
328 return 0;
329 saw_digit = 1;
330 }
331 } else if (ch == '.' && saw_digit) {
332 if (octets == 4)
333 return 0;
334 *++tp = 0;
335 saw_digit = 0;
336 } else
337 return 0;
338 }
339
340 if (octets < 4)
341 return 0;
342
343 memcpy(&dst->s_addr, tmp, 4);
344 return addr-cp-1;
345}
346
347/*
348 * Resolve destination server from URL. Convert <str> to a sockaddr_in*.
349 */
350int url2sa(const char *url, int ulen, struct sockaddr_in *addr)
351{
352 const char *curr = url, *cp = url;
353 int ret, url_code = 0;
354 unsigned int http_code = 0;
355
356 /* Cleanup the room */
357 addr->sin_family = AF_INET;
358 addr->sin_addr.s_addr = 0;
359 addr->sin_port = 0;
360
361 /* Firstly, try to find :// pattern */
362 while (curr < url+ulen && url_code != 0x3a2f2f) {
363 url_code = ((url_code & 0xffff) << 8);
364 url_code += (unsigned char)*curr++;
365 }
366
367 /* Secondly, if :// pattern is found, verify parsed stuff
368 * before pattern is matching our http pattern.
369 * If so parse ip address and port in uri.
370 *
371 * WARNING: Current code doesn't support dynamic async dns resolver.
372 */
373 if (url_code == 0x3a2f2f) {
374 while (cp < curr - 3)
375 http_code = (http_code << 8) + *cp++;
376 http_code |= 0x20202020; /* Turn everything to lower case */
377
378 /* HTTP url matching */
379 if (http_code == 0x68747470) {
380 /* We are looking for IP address. If you want to parse and
381 * resolve hostname found in url, you can use str2sa(), but
382 * be warned this can slow down global daemon performances
383 * while handling lagging dns responses.
384 */
385 ret = url2ip(curr, &addr->sin_addr);
386 if (!ret)
387 return -1;
388 curr += ret;
Willy Tarreaud1cd2762007-12-02 10:55:56 +0100389 addr->sin_port = (*curr == ':') ? str2uic(++curr) : 80;
390 addr->sin_port = htons(addr->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100391 }
392 return 0;
393 }
394
395 return -1;
396}
397
Willy Tarreaubaaee002006-06-26 02:48:02 +0200398/* will try to encode the string <string> replacing all characters tagged in
399 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
400 * prefixed by <escape>, and will store the result between <start> (included)
401 * and <stop> (excluded), and will always terminate the string with a '\0'
402 * before <stop>. The position of the '\0' is returned if the conversion
403 * completes. If bytes are missing between <start> and <stop>, then the
404 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
405 * cannot even be stored so we return <start> without writing the 0.
406 * The input string must also be zero-terminated.
407 */
408const char hextab[16] = "0123456789ABCDEF";
409char *encode_string(char *start, char *stop,
410 const char escape, const fd_set *map,
411 const char *string)
412{
413 if (start < stop) {
414 stop--; /* reserve one byte for the final '\0' */
415 while (start < stop && *string != '\0') {
416 if (!FD_ISSET((unsigned char)(*string), map))
417 *start++ = *string;
418 else {
419 if (start + 3 >= stop)
420 break;
421 *start++ = escape;
422 *start++ = hextab[(*string >> 4) & 15];
423 *start++ = hextab[*string & 15];
424 }
425 string++;
426 }
427 *start = '\0';
428 }
429 return start;
430}
431
432
Willy Tarreau6911fa42007-03-04 18:06:08 +0100433unsigned int str2ui(const char *s)
434{
435 return __str2ui(s);
436}
437
438unsigned int str2uic(const char *s)
439{
440 return __str2uic(s);
441}
442
443unsigned int strl2ui(const char *s, int len)
444{
445 return __strl2ui(s, len);
446}
447
448unsigned int strl2uic(const char *s, int len)
449{
450 return __strl2uic(s, len);
451}
452
453/* This one is 7 times faster than strtol() on athlon with checks.
454 * It returns the value of the number composed of all valid digits read,
455 * and can process negative numbers too.
456 */
457int strl2ic(const char *s, int len)
458{
459 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200460 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100461
462 if (len > 0) {
463 if (*s != '-') {
464 /* positive number */
465 while (len-- > 0) {
466 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200467 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100468 if (j > 9)
469 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200470 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100471 }
472 } else {
473 /* negative number */
474 s++;
475 while (--len > 0) {
476 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200477 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100478 if (j > 9)
479 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200480 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100481 }
482 }
483 }
484 return i;
485}
486
487
488/* This function reads exactly <len> chars from <s> and converts them to a
489 * signed integer which it stores into <ret>. It accurately detects any error
490 * (truncated string, invalid chars, overflows). It is meant to be used in
491 * applications designed for hostile environments. It returns zero when the
492 * number has successfully been converted, non-zero otherwise. When an error
493 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
494 * faster than strtol().
495 */
496int strl2irc(const char *s, int len, int *ret)
497{
498 int i = 0;
499 int j;
500
501 if (!len)
502 return 1;
503
504 if (*s != '-') {
505 /* positive number */
506 while (len-- > 0) {
507 j = (*s++) - '0';
508 if (j > 9) return 1; /* invalid char */
509 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
510 i = i * 10;
511 if (i + j < i) return 1; /* check for addition overflow */
512 i = i + j;
513 }
514 } else {
515 /* negative number */
516 s++;
517 while (--len > 0) {
518 j = (*s++) - '0';
519 if (j > 9) return 1; /* invalid char */
520 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
521 i = i * 10;
522 if (i - j > i) return 1; /* check for subtract overflow */
523 i = i - j;
524 }
525 }
526 *ret = i;
527 return 0;
528}
529
530
531/* This function reads exactly <len> chars from <s> and converts them to a
532 * signed integer which it stores into <ret>. It accurately detects any error
533 * (truncated string, invalid chars, overflows). It is meant to be used in
534 * applications designed for hostile environments. It returns zero when the
535 * number has successfully been converted, non-zero otherwise. When an error
536 * is returned, the <ret> value is left untouched. It is about 3 times slower
537 * than str2irc().
538 */
539#ifndef LLONG_MAX
540#define LLONG_MAX 9223372036854775807LL
541#define LLONG_MIN (-LLONG_MAX - 1LL)
542#endif
543
544int strl2llrc(const char *s, int len, long long *ret)
545{
546 long long i = 0;
547 int j;
548
549 if (!len)
550 return 1;
551
552 if (*s != '-') {
553 /* positive number */
554 while (len-- > 0) {
555 j = (*s++) - '0';
556 if (j > 9) return 1; /* invalid char */
557 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
558 i = i * 10LL;
559 if (i + j < i) return 1; /* check for addition overflow */
560 i = i + j;
561 }
562 } else {
563 /* negative number */
564 s++;
565 while (--len > 0) {
566 j = (*s++) - '0';
567 if (j > 9) return 1; /* invalid char */
568 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
569 i = i * 10LL;
570 if (i - j > i) return 1; /* check for subtract overflow */
571 i = i - j;
572 }
573 }
574 *ret = i;
575 return 0;
576}
577
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100578/* This function parses a time value optionally followed by a unit suffix among
579 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
580 * expected by the caller. The computation does its best to avoid overflows.
581 * The value is returned in <ret> if everything is fine, and a NULL is returned
582 * by the function. In case of error, a pointer to the error is returned and
583 * <ret> is left untouched. Values are automatically rounded up when needed.
584 */
585const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
586{
587 unsigned imult, idiv;
588 unsigned omult, odiv;
589 unsigned value;
590
591 omult = odiv = 1;
592
593 switch (unit_flags & TIME_UNIT_MASK) {
594 case TIME_UNIT_US: omult = 1000000; break;
595 case TIME_UNIT_MS: omult = 1000; break;
596 case TIME_UNIT_S: break;
597 case TIME_UNIT_MIN: odiv = 60; break;
598 case TIME_UNIT_HOUR: odiv = 3600; break;
599 case TIME_UNIT_DAY: odiv = 86400; break;
600 default: break;
601 }
602
603 value = 0;
604
605 while (1) {
606 unsigned int j;
607
608 j = *text - '0';
609 if (j > 9)
610 break;
611 text++;
612 value *= 10;
613 value += j;
614 }
615
616 imult = idiv = 1;
617 switch (*text) {
618 case '\0': /* no unit = default unit */
619 imult = omult = idiv = odiv = 1;
620 break;
621 case 's': /* second = unscaled unit */
622 break;
623 case 'u': /* microsecond : "us" */
624 if (text[1] == 's') {
625 idiv = 1000000;
626 text++;
627 }
628 break;
629 case 'm': /* millisecond : "ms" or minute: "m" */
630 if (text[1] == 's') {
631 idiv = 1000;
632 text++;
633 } else
634 imult = 60;
635 break;
636 case 'h': /* hour : "h" */
637 imult = 3600;
638 break;
639 case 'd': /* day : "d" */
640 imult = 86400;
641 break;
642 default:
643 return text;
644 break;
645 }
646
647 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
648 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
649 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
650 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
651
652 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
653 *ret = value;
654 return NULL;
655}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100656
Willy Tarreaubaaee002006-06-26 02:48:02 +0200657/*
658 * Local variables:
659 * c-indent-level: 8
660 * c-basic-offset: 8
661 * End:
662 */