blob: 9dc56afbf7dafc278f19b8aa9676029061d29d57 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General purpose functions.
3 *
Willy Tarreauc6f4ce82009-06-10 11:09:37 +02004 * Copyright 2000-2009 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 Tarreauc6f4ce82009-06-10 11:09:37 +0200244 * converts <str> to a struct sockaddr_in* which is locally allocated, and a
245 * port range consisting in two integers. The low and high end are always set
246 * even if the port is unspecified, in which case (0,0) is returned. The low
247 * port is set in the sockaddr_in. Thus, it is enough to check the size of the
248 * returned range to know if an array must be allocated or not. The format is
249 * "addr[:port[-port]]", where "addr" can be a dotted IPv4 address, a host
250 * name, or empty or "*" to indicate INADDR_ANY.
251 */
252struct sockaddr_in *str2sa_range(char *str, int *low, int *high)
253{
254 static struct sockaddr_in sa;
255 char *c;
256 int portl, porth;
257
258 memset(&sa, 0, sizeof(sa));
259 str = strdup(str);
260 if (str == NULL)
261 goto out_nofree;
262
263 if ((c = strrchr(str,':')) != NULL) {
264 char *sep;
265 *c++ = '\0';
266 sep = strchr(c, '-');
267 if (sep)
268 *sep++ = '\0';
269 else
270 sep = c;
271 portl = atol(c);
272 porth = atol(sep);
273 }
274 else {
275 portl = 0;
276 porth = 0;
277 }
278
279 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
280 sa.sin_addr.s_addr = INADDR_ANY;
281 }
282 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
283 struct hostent *he;
284
285 if ((he = gethostbyname(str)) == NULL) {
286 Alert("Invalid server name: '%s'\n", str);
287 }
288 else
289 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
290 }
291 sa.sin_port = htons(portl);
292 sa.sin_family = AF_INET;
293
294 *low = portl;
295 *high = porth;
296
297 free(str);
298 out_nofree:
299 return &sa;
300}
301
302/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200303 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200304 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
305 * is optionnal and either in the dotted or CIDR notation.
306 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
307 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200308int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200309{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200310 __label__ out_free, out_err;
311 char *c, *s;
312 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200313 unsigned long len;
314
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200315 s = strdup(str);
316 if (!s)
317 return 0;
318
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319 memset(mask, 0, sizeof(*mask));
320 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200321
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200322 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200323 *c++ = '\0';
324 /* c points to the mask */
325 if (strchr(c, '.') != NULL) { /* dotted notation */
326 if (!inet_pton(AF_INET, c, mask))
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200327 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200328 }
329 else { /* mask length */
330 char *err;
331 len = strtol(c, &err, 10);
332 if (!*c || (err && *err) || (unsigned)len > 32)
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200333 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334 if (len)
335 mask->s_addr = htonl(~0UL << (32 - len));
336 else
337 mask->s_addr = 0;
338 }
339 }
340 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100341 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200342 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200343 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200344 struct hostent *he;
345
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200346 if ((he = gethostbyname(s)) == NULL) {
347 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348 }
349 else
350 *addr = *(struct in_addr *) *(he->h_addr_list);
351 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200352
353 ret_val = 1;
354 out_free:
355 free(s);
356 return ret_val;
357 out_err:
358 ret_val = 0;
359 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360}
361
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100362
363/*
364 * Parse IP address found in url.
365 */
366static int url2ip(const char *addr, struct in_addr *dst)
367{
368 int saw_digit, octets, ch;
369 u_char tmp[4], *tp;
370 const char *cp = addr;
371
372 saw_digit = 0;
373 octets = 0;
374 *(tp = tmp) = 0;
375
376 while (*addr) {
377 unsigned char digit = (ch = *addr++) - '0';
378 if (digit > 9 && ch != '.')
379 break;
380 if (digit <= 9) {
381 u_int new = *tp * 10 + digit;
382 if (new > 255)
383 return 0;
384 *tp = new;
385 if (!saw_digit) {
386 if (++octets > 4)
387 return 0;
388 saw_digit = 1;
389 }
390 } else if (ch == '.' && saw_digit) {
391 if (octets == 4)
392 return 0;
393 *++tp = 0;
394 saw_digit = 0;
395 } else
396 return 0;
397 }
398
399 if (octets < 4)
400 return 0;
401
402 memcpy(&dst->s_addr, tmp, 4);
403 return addr-cp-1;
404}
405
406/*
407 * Resolve destination server from URL. Convert <str> to a sockaddr_in*.
408 */
409int url2sa(const char *url, int ulen, struct sockaddr_in *addr)
410{
411 const char *curr = url, *cp = url;
412 int ret, url_code = 0;
413 unsigned int http_code = 0;
414
415 /* Cleanup the room */
416 addr->sin_family = AF_INET;
417 addr->sin_addr.s_addr = 0;
418 addr->sin_port = 0;
419
420 /* Firstly, try to find :// pattern */
421 while (curr < url+ulen && url_code != 0x3a2f2f) {
422 url_code = ((url_code & 0xffff) << 8);
423 url_code += (unsigned char)*curr++;
424 }
425
426 /* Secondly, if :// pattern is found, verify parsed stuff
427 * before pattern is matching our http pattern.
428 * If so parse ip address and port in uri.
429 *
430 * WARNING: Current code doesn't support dynamic async dns resolver.
431 */
432 if (url_code == 0x3a2f2f) {
433 while (cp < curr - 3)
434 http_code = (http_code << 8) + *cp++;
435 http_code |= 0x20202020; /* Turn everything to lower case */
436
437 /* HTTP url matching */
438 if (http_code == 0x68747470) {
439 /* We are looking for IP address. If you want to parse and
440 * resolve hostname found in url, you can use str2sa(), but
441 * be warned this can slow down global daemon performances
442 * while handling lagging dns responses.
443 */
444 ret = url2ip(curr, &addr->sin_addr);
445 if (!ret)
446 return -1;
447 curr += ret;
Willy Tarreaud1cd2762007-12-02 10:55:56 +0100448 addr->sin_port = (*curr == ':') ? str2uic(++curr) : 80;
449 addr->sin_port = htons(addr->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100450 }
451 return 0;
452 }
453
454 return -1;
455}
456
Willy Tarreaubaaee002006-06-26 02:48:02 +0200457/* will try to encode the string <string> replacing all characters tagged in
458 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
459 * prefixed by <escape>, and will store the result between <start> (included)
460 * and <stop> (excluded), and will always terminate the string with a '\0'
461 * before <stop>. The position of the '\0' is returned if the conversion
462 * completes. If bytes are missing between <start> and <stop>, then the
463 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
464 * cannot even be stored so we return <start> without writing the 0.
465 * The input string must also be zero-terminated.
466 */
467const char hextab[16] = "0123456789ABCDEF";
468char *encode_string(char *start, char *stop,
469 const char escape, const fd_set *map,
470 const char *string)
471{
472 if (start < stop) {
473 stop--; /* reserve one byte for the final '\0' */
474 while (start < stop && *string != '\0') {
475 if (!FD_ISSET((unsigned char)(*string), map))
476 *start++ = *string;
477 else {
478 if (start + 3 >= stop)
479 break;
480 *start++ = escape;
481 *start++ = hextab[(*string >> 4) & 15];
482 *start++ = hextab[*string & 15];
483 }
484 string++;
485 }
486 *start = '\0';
487 }
488 return start;
489}
490
491
Willy Tarreau6911fa42007-03-04 18:06:08 +0100492unsigned int str2ui(const char *s)
493{
494 return __str2ui(s);
495}
496
497unsigned int str2uic(const char *s)
498{
499 return __str2uic(s);
500}
501
502unsigned int strl2ui(const char *s, int len)
503{
504 return __strl2ui(s, len);
505}
506
507unsigned int strl2uic(const char *s, int len)
508{
509 return __strl2uic(s, len);
510}
511
512/* This one is 7 times faster than strtol() on athlon with checks.
513 * It returns the value of the number composed of all valid digits read,
514 * and can process negative numbers too.
515 */
516int strl2ic(const char *s, int len)
517{
518 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200519 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100520
521 if (len > 0) {
522 if (*s != '-') {
523 /* positive number */
524 while (len-- > 0) {
525 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200526 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100527 if (j > 9)
528 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200529 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100530 }
531 } else {
532 /* negative number */
533 s++;
534 while (--len > 0) {
535 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200536 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100537 if (j > 9)
538 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200539 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100540 }
541 }
542 }
543 return i;
544}
545
546
547/* This function reads exactly <len> chars from <s> and converts them to a
548 * signed integer which it stores into <ret>. It accurately detects any error
549 * (truncated string, invalid chars, overflows). It is meant to be used in
550 * applications designed for hostile environments. It returns zero when the
551 * number has successfully been converted, non-zero otherwise. When an error
552 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
553 * faster than strtol().
554 */
555int strl2irc(const char *s, int len, int *ret)
556{
557 int i = 0;
558 int j;
559
560 if (!len)
561 return 1;
562
563 if (*s != '-') {
564 /* positive number */
565 while (len-- > 0) {
566 j = (*s++) - '0';
567 if (j > 9) return 1; /* invalid char */
568 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
569 i = i * 10;
570 if (i + j < i) return 1; /* check for addition overflow */
571 i = i + j;
572 }
573 } else {
574 /* negative number */
575 s++;
576 while (--len > 0) {
577 j = (*s++) - '0';
578 if (j > 9) return 1; /* invalid char */
579 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
580 i = i * 10;
581 if (i - j > i) return 1; /* check for subtract overflow */
582 i = i - j;
583 }
584 }
585 *ret = i;
586 return 0;
587}
588
589
590/* This function reads exactly <len> chars from <s> and converts them to a
591 * signed integer which it stores into <ret>. It accurately detects any error
592 * (truncated string, invalid chars, overflows). It is meant to be used in
593 * applications designed for hostile environments. It returns zero when the
594 * number has successfully been converted, non-zero otherwise. When an error
595 * is returned, the <ret> value is left untouched. It is about 3 times slower
596 * than str2irc().
597 */
598#ifndef LLONG_MAX
599#define LLONG_MAX 9223372036854775807LL
600#define LLONG_MIN (-LLONG_MAX - 1LL)
601#endif
602
603int strl2llrc(const char *s, int len, long long *ret)
604{
605 long long i = 0;
606 int j;
607
608 if (!len)
609 return 1;
610
611 if (*s != '-') {
612 /* positive number */
613 while (len-- > 0) {
614 j = (*s++) - '0';
615 if (j > 9) return 1; /* invalid char */
616 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
617 i = i * 10LL;
618 if (i + j < i) return 1; /* check for addition overflow */
619 i = i + j;
620 }
621 } else {
622 /* negative number */
623 s++;
624 while (--len > 0) {
625 j = (*s++) - '0';
626 if (j > 9) return 1; /* invalid char */
627 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
628 i = i * 10LL;
629 if (i - j > i) return 1; /* check for subtract overflow */
630 i = i - j;
631 }
632 }
633 *ret = i;
634 return 0;
635}
636
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100637/* This function parses a time value optionally followed by a unit suffix among
638 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
639 * expected by the caller. The computation does its best to avoid overflows.
640 * The value is returned in <ret> if everything is fine, and a NULL is returned
641 * by the function. In case of error, a pointer to the error is returned and
642 * <ret> is left untouched. Values are automatically rounded up when needed.
643 */
644const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
645{
646 unsigned imult, idiv;
647 unsigned omult, odiv;
648 unsigned value;
649
650 omult = odiv = 1;
651
652 switch (unit_flags & TIME_UNIT_MASK) {
653 case TIME_UNIT_US: omult = 1000000; break;
654 case TIME_UNIT_MS: omult = 1000; break;
655 case TIME_UNIT_S: break;
656 case TIME_UNIT_MIN: odiv = 60; break;
657 case TIME_UNIT_HOUR: odiv = 3600; break;
658 case TIME_UNIT_DAY: odiv = 86400; break;
659 default: break;
660 }
661
662 value = 0;
663
664 while (1) {
665 unsigned int j;
666
667 j = *text - '0';
668 if (j > 9)
669 break;
670 text++;
671 value *= 10;
672 value += j;
673 }
674
675 imult = idiv = 1;
676 switch (*text) {
677 case '\0': /* no unit = default unit */
678 imult = omult = idiv = odiv = 1;
679 break;
680 case 's': /* second = unscaled unit */
681 break;
682 case 'u': /* microsecond : "us" */
683 if (text[1] == 's') {
684 idiv = 1000000;
685 text++;
686 }
687 break;
688 case 'm': /* millisecond : "ms" or minute: "m" */
689 if (text[1] == 's') {
690 idiv = 1000;
691 text++;
692 } else
693 imult = 60;
694 break;
695 case 'h': /* hour : "h" */
696 imult = 3600;
697 break;
698 case 'd': /* day : "d" */
699 imult = 86400;
700 break;
701 default:
702 return text;
703 break;
704 }
705
706 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
707 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
708 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
709 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
710
711 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
712 *ret = value;
713 return NULL;
714}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100715
Willy Tarreau946ba592009-05-10 15:41:18 +0200716/* copies at most <n> characters from <src> and always terminates with '\0' */
717char *my_strndup(const char *src, int n)
718{
719 int len = 0;
720 char *ret;
721
722 while (len < n && src[len])
723 len++;
724
725 ret = (char *)malloc(len + 1);
726 if (!ret)
727 return ret;
728 memcpy(ret, src, len);
729 ret[len] = '\0';
730 return ret;
731}
732
Willy Tarreaubaaee002006-06-26 02:48:02 +0200733/*
734 * Local variables:
735 * c-indent-level: 8
736 * c-basic-offset: 8
737 * End:
738 */