blob: aab318bb36c894e85363b70287261f2688a05dda [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 Tarreau45cb4fb2009-10-26 21:10:04 +010024#include <eb32tree.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <proto/log.h>
26
Willy Tarreau72d759c2007-10-25 12:14:10 +020027/* enough to store 10 integers of :
28 * 2^64-1 = 18446744073709551615 or
29 * -2^63 = -9223372036854775808
Willy Tarreaue7239b52009-03-29 13:41:58 +020030 *
31 * The HTML version needs room for adding the 25 characters
32 * '<span class="rls"></span>' around digits at positions 3N+1 in order
33 * to add spacing at up to 6 positions : 18 446 744 073 709 551 615
Willy Tarreau72d759c2007-10-25 12:14:10 +020034 */
Willy Tarreaue7239b52009-03-29 13:41:58 +020035char itoa_str[10][171];
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
37/*
38 * copies at most <size-1> chars from <src> to <dst>. Last char is always
39 * set to 0, unless <size> is 0. The number of chars copied is returned
40 * (excluding the terminating zero).
41 * This code has been optimized for size and speed : on x86, it's 45 bytes
42 * long, uses only registers, and consumes only 4 cycles per char.
43 */
44int strlcpy2(char *dst, const char *src, int size)
45{
46 char *orig = dst;
47 if (size) {
48 while (--size && (*dst = *src)) {
49 src++; dst++;
50 }
51 *dst = 0;
52 }
53 return dst - orig;
54}
55
56/*
Willy Tarreau72d759c2007-10-25 12:14:10 +020057 * This function simply returns a locally allocated string containing
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 * the ascii representation for number 'n' in decimal.
59 */
Willy Tarreau72d759c2007-10-25 12:14:10 +020060const char *ultoa_r(unsigned long n, char *buffer, int size)
Willy Tarreaubaaee002006-06-26 02:48:02 +020061{
62 char *pos;
63
Willy Tarreau72d759c2007-10-25 12:14:10 +020064 pos = buffer + size - 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020065 *pos-- = '\0';
66
67 do {
68 *pos-- = '0' + n % 10;
69 n /= 10;
Willy Tarreau72d759c2007-10-25 12:14:10 +020070 } while (n && pos >= buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +020071 return pos + 1;
72}
73
Willy Tarreau91092e52007-10-25 16:58:42 +020074/*
Willy Tarreaue7239b52009-03-29 13:41:58 +020075 * This function simply returns a locally allocated string containing
76 * the ascii representation for number 'n' in decimal, formatted for
77 * HTML output with tags to create visual grouping by 3 digits. The
78 * output needs to support at least 171 characters.
79 */
80const char *ulltoh_r(unsigned long long n, char *buffer, int size)
81{
82 char *start;
83 int digit = 0;
84
85 start = buffer + size;
86 *--start = '\0';
87
88 do {
89 if (digit == 3 && start >= buffer + 7)
90 memcpy(start -= 7, "</span>", 7);
91
92 if (start >= buffer + 1) {
93 *--start = '0' + n % 10;
94 n /= 10;
95 }
96
97 if (digit == 3 && start >= buffer + 18)
98 memcpy(start -= 18, "<span class=\"rls\">", 18);
99
100 if (digit++ == 3)
101 digit = 1;
102 } while (n && start > buffer);
103 return start;
104}
105
106/*
Willy Tarreau91092e52007-10-25 16:58:42 +0200107 * This function simply returns a locally allocated string containing the ascii
108 * representation for number 'n' in decimal, unless n is 0 in which case it
109 * returns the alternate string (or an empty string if the alternate string is
110 * NULL). It use is intended for limits reported in reports, where it's
111 * desirable not to display anything if there is no limit. Warning! it shares
112 * the same vector as ultoa_r().
113 */
114const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
115{
116 return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
117}
118
Robert Tsai81ae1952007-12-05 10:47:29 +0100119/*
120 * converts <str> to a struct sockaddr_un* which is locally allocated.
121 * The format is "/path", where "/path" is a path to a UNIX domain socket.
122 */
Willy Tarreaucaf720d2008-03-07 10:07:04 +0100123struct sockaddr_un *str2sun(const char *str)
Robert Tsai81ae1952007-12-05 10:47:29 +0100124{
Willy Tarreau127f9662007-12-06 00:53:51 +0100125 static struct sockaddr_un su;
Robert Tsai81ae1952007-12-05 10:47:29 +0100126 int strsz; /* length included null */
127
Willy Tarreau127f9662007-12-06 00:53:51 +0100128 memset(&su, 0, sizeof(su));
Robert Tsai81ae1952007-12-05 10:47:29 +0100129 strsz = strlen(str) + 1;
Willy Tarreau127f9662007-12-06 00:53:51 +0100130 if (strsz > sizeof(su.sun_path)) {
Robert Tsai81ae1952007-12-05 10:47:29 +0100131 Alert("Socket path '%s' too long (max %d)\n",
Willy Tarreau5e4a6f12009-04-11 19:42:49 +0200132 str, (int)sizeof(su.sun_path) - 1);
Willy Tarreaucaf720d2008-03-07 10:07:04 +0100133 } else {
134 su.sun_family = AF_UNIX;
135 memcpy(su.sun_path, str, strsz);
Robert Tsai81ae1952007-12-05 10:47:29 +0100136 }
Willy Tarreau127f9662007-12-06 00:53:51 +0100137 return &su;
Robert Tsai81ae1952007-12-05 10:47:29 +0100138}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139
140/*
141 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
142 *
143 * It looks like this one would be a good candidate for inlining, but this is
144 * not interesting because it around 35 bytes long and often called multiple
145 * times within the same function.
146 */
147int ishex(char s)
148{
149 s -= '0';
150 if ((unsigned char)s <= 9)
151 return 1;
152 s -= 'A' - '0';
153 if ((unsigned char)s <= 5)
154 return 1;
155 s -= 'a' - 'A';
156 if ((unsigned char)s <= 5)
157 return 1;
158 return 0;
159}
160
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100161/*
Willy Tarreauda3b7c32009-11-02 20:12:52 +0100162 * Return integer equivalent of character <c> for a hex digit (0-9, a-f, A-F),
163 * otherwise -1. This compact form helps gcc produce efficient code.
164 */
165int hex2i(int c)
166{
167 if ((unsigned char)(c -= '0') > 9) {
168 if ((unsigned char)(c -= 'A' - '0') > 5 &&
169 (unsigned char)(c -= 'a' - 'A') > 5)
170 c = -11;
171 c += 10;
172 }
173 return c;
174}
175
176/*
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100177 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
178 * invalid character is found, a pointer to it is returned. If everything is
179 * fine, NULL is returned.
180 */
181const char *invalid_char(const char *name)
182{
183 if (!*name)
184 return name;
185
186 while (*name) {
Willy Tarreau127f9662007-12-06 00:53:51 +0100187 if (!isalnum((int)*name) && *name != '.' && *name != ':' &&
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100188 *name != '_' && *name != '-')
189 return name;
190 name++;
191 }
192 return NULL;
193}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200194
195/*
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200196 * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-].
197 * If an invalid character is found, a pointer to it is returned.
198 * If everything is fine, NULL is returned.
199 */
200const char *invalid_domainchar(const char *name) {
201
202 if (!*name)
203 return name;
204
205 while (*name) {
206 if (!isalnum((int)*name) && *name != '.' &&
207 *name != '_' && *name != '-')
208 return name;
209
210 name++;
211 }
212
213 return NULL;
214}
215
216/*
Willy Tarreaubaaee002006-06-26 02:48:02 +0200217 * converts <str> to a struct sockaddr_in* which is locally allocated.
218 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
219 * a host name, or empty or "*" to indicate INADDR_ANY.
220 */
221struct sockaddr_in *str2sa(char *str)
222{
223 static struct sockaddr_in sa;
224 char *c;
225 int port;
226
227 memset(&sa, 0, sizeof(sa));
228 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200229 if (str == NULL)
230 goto out_nofree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200231
232 if ((c = strrchr(str,':')) != NULL) {
233 *c++ = '\0';
234 port = atol(c);
235 }
236 else
237 port = 0;
238
239 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
240 sa.sin_addr.s_addr = INADDR_ANY;
241 }
242 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
243 struct hostent *he;
244
245 if ((he = gethostbyname(str)) == NULL) {
246 Alert("Invalid server name: '%s'\n", str);
247 }
248 else
249 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
250 }
251 sa.sin_port = htons(port);
252 sa.sin_family = AF_INET;
253
254 free(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200255 out_nofree:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256 return &sa;
257}
258
259/*
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200260 * converts <str> to a struct sockaddr_in* which is locally allocated, and a
261 * port range consisting in two integers. The low and high end are always set
262 * even if the port is unspecified, in which case (0,0) is returned. The low
263 * port is set in the sockaddr_in. Thus, it is enough to check the size of the
264 * returned range to know if an array must be allocated or not. The format is
265 * "addr[:port[-port]]", where "addr" can be a dotted IPv4 address, a host
266 * name, or empty or "*" to indicate INADDR_ANY.
267 */
268struct sockaddr_in *str2sa_range(char *str, int *low, int *high)
269{
270 static struct sockaddr_in sa;
271 char *c;
272 int portl, porth;
273
274 memset(&sa, 0, sizeof(sa));
275 str = strdup(str);
276 if (str == NULL)
277 goto out_nofree;
278
279 if ((c = strrchr(str,':')) != NULL) {
280 char *sep;
281 *c++ = '\0';
282 sep = strchr(c, '-');
283 if (sep)
284 *sep++ = '\0';
285 else
286 sep = c;
287 portl = atol(c);
288 porth = atol(sep);
289 }
290 else {
291 portl = 0;
292 porth = 0;
293 }
294
295 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
296 sa.sin_addr.s_addr = INADDR_ANY;
297 }
298 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
299 struct hostent *he;
300
301 if ((he = gethostbyname(str)) == NULL) {
302 Alert("Invalid server name: '%s'\n", str);
303 }
304 else
305 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
306 }
307 sa.sin_port = htons(portl);
308 sa.sin_family = AF_INET;
309
310 *low = portl;
311 *high = porth;
312
313 free(str);
314 out_nofree:
315 return &sa;
316}
317
318/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200319 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
321 * is optionnal and either in the dotted or CIDR notation.
322 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
323 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200324int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200325{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200326 __label__ out_free, out_err;
327 char *c, *s;
328 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200329 unsigned long len;
330
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200331 s = strdup(str);
332 if (!s)
333 return 0;
334
Willy Tarreaubaaee002006-06-26 02:48:02 +0200335 memset(mask, 0, sizeof(*mask));
336 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200337
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200338 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200339 *c++ = '\0';
340 /* c points to the mask */
341 if (strchr(c, '.') != NULL) { /* dotted notation */
342 if (!inet_pton(AF_INET, c, mask))
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200343 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200344 }
345 else { /* mask length */
346 char *err;
347 len = strtol(c, &err, 10);
348 if (!*c || (err && *err) || (unsigned)len > 32)
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200349 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200350 if (len)
351 mask->s_addr = htonl(~0UL << (32 - len));
352 else
353 mask->s_addr = 0;
354 }
355 }
356 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100357 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200358 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200359 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200360 struct hostent *he;
361
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200362 if ((he = gethostbyname(s)) == NULL) {
363 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200364 }
365 else
366 *addr = *(struct in_addr *) *(he->h_addr_list);
367 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200368
369 ret_val = 1;
370 out_free:
371 free(s);
372 return ret_val;
373 out_err:
374 ret_val = 0;
375 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200376}
377
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100378
379/*
380 * Parse IP address found in url.
381 */
Willy Tarreau106f9792009-09-19 07:54:16 +0200382int url2ip(const char *addr, struct in_addr *dst)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100383{
384 int saw_digit, octets, ch;
385 u_char tmp[4], *tp;
386 const char *cp = addr;
387
388 saw_digit = 0;
389 octets = 0;
390 *(tp = tmp) = 0;
391
392 while (*addr) {
393 unsigned char digit = (ch = *addr++) - '0';
394 if (digit > 9 && ch != '.')
395 break;
396 if (digit <= 9) {
397 u_int new = *tp * 10 + digit;
398 if (new > 255)
399 return 0;
400 *tp = new;
401 if (!saw_digit) {
402 if (++octets > 4)
403 return 0;
404 saw_digit = 1;
405 }
406 } else if (ch == '.' && saw_digit) {
407 if (octets == 4)
408 return 0;
409 *++tp = 0;
410 saw_digit = 0;
411 } else
412 return 0;
413 }
414
415 if (octets < 4)
416 return 0;
417
418 memcpy(&dst->s_addr, tmp, 4);
419 return addr-cp-1;
420}
421
422/*
423 * Resolve destination server from URL. Convert <str> to a sockaddr_in*.
424 */
425int url2sa(const char *url, int ulen, struct sockaddr_in *addr)
426{
427 const char *curr = url, *cp = url;
428 int ret, url_code = 0;
429 unsigned int http_code = 0;
430
431 /* Cleanup the room */
432 addr->sin_family = AF_INET;
433 addr->sin_addr.s_addr = 0;
434 addr->sin_port = 0;
435
436 /* Firstly, try to find :// pattern */
437 while (curr < url+ulen && url_code != 0x3a2f2f) {
438 url_code = ((url_code & 0xffff) << 8);
439 url_code += (unsigned char)*curr++;
440 }
441
442 /* Secondly, if :// pattern is found, verify parsed stuff
443 * before pattern is matching our http pattern.
444 * If so parse ip address and port in uri.
445 *
446 * WARNING: Current code doesn't support dynamic async dns resolver.
447 */
448 if (url_code == 0x3a2f2f) {
449 while (cp < curr - 3)
450 http_code = (http_code << 8) + *cp++;
451 http_code |= 0x20202020; /* Turn everything to lower case */
452
453 /* HTTP url matching */
454 if (http_code == 0x68747470) {
455 /* We are looking for IP address. If you want to parse and
456 * resolve hostname found in url, you can use str2sa(), but
457 * be warned this can slow down global daemon performances
458 * while handling lagging dns responses.
459 */
460 ret = url2ip(curr, &addr->sin_addr);
461 if (!ret)
462 return -1;
463 curr += ret;
Willy Tarreaud1cd2762007-12-02 10:55:56 +0100464 addr->sin_port = (*curr == ':') ? str2uic(++curr) : 80;
465 addr->sin_port = htons(addr->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100466 }
467 return 0;
468 }
469
470 return -1;
471}
472
Willy Tarreaubaaee002006-06-26 02:48:02 +0200473/* will try to encode the string <string> replacing all characters tagged in
474 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
475 * prefixed by <escape>, and will store the result between <start> (included)
476 * and <stop> (excluded), and will always terminate the string with a '\0'
477 * before <stop>. The position of the '\0' is returned if the conversion
478 * completes. If bytes are missing between <start> and <stop>, then the
479 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
480 * cannot even be stored so we return <start> without writing the 0.
481 * The input string must also be zero-terminated.
482 */
483const char hextab[16] = "0123456789ABCDEF";
484char *encode_string(char *start, char *stop,
485 const char escape, const fd_set *map,
486 const char *string)
487{
488 if (start < stop) {
489 stop--; /* reserve one byte for the final '\0' */
490 while (start < stop && *string != '\0') {
491 if (!FD_ISSET((unsigned char)(*string), map))
492 *start++ = *string;
493 else {
494 if (start + 3 >= stop)
495 break;
496 *start++ = escape;
497 *start++ = hextab[(*string >> 4) & 15];
498 *start++ = hextab[*string & 15];
499 }
500 string++;
501 }
502 *start = '\0';
503 }
504 return start;
505}
506
507
Willy Tarreau6911fa42007-03-04 18:06:08 +0100508unsigned int str2ui(const char *s)
509{
510 return __str2ui(s);
511}
512
513unsigned int str2uic(const char *s)
514{
515 return __str2uic(s);
516}
517
518unsigned int strl2ui(const char *s, int len)
519{
520 return __strl2ui(s, len);
521}
522
523unsigned int strl2uic(const char *s, int len)
524{
525 return __strl2uic(s, len);
526}
527
528/* This one is 7 times faster than strtol() on athlon with checks.
529 * It returns the value of the number composed of all valid digits read,
530 * and can process negative numbers too.
531 */
532int strl2ic(const char *s, int len)
533{
534 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200535 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100536
537 if (len > 0) {
538 if (*s != '-') {
539 /* positive number */
540 while (len-- > 0) {
541 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200542 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100543 if (j > 9)
544 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200545 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100546 }
547 } else {
548 /* negative number */
549 s++;
550 while (--len > 0) {
551 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200552 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100553 if (j > 9)
554 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200555 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100556 }
557 }
558 }
559 return i;
560}
561
562
563/* This function reads exactly <len> chars from <s> and converts them to a
564 * signed integer which it stores into <ret>. It accurately detects any error
565 * (truncated string, invalid chars, overflows). It is meant to be used in
566 * applications designed for hostile environments. It returns zero when the
567 * number has successfully been converted, non-zero otherwise. When an error
568 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
569 * faster than strtol().
570 */
571int strl2irc(const char *s, int len, int *ret)
572{
573 int i = 0;
574 int j;
575
576 if (!len)
577 return 1;
578
579 if (*s != '-') {
580 /* positive number */
581 while (len-- > 0) {
582 j = (*s++) - '0';
583 if (j > 9) return 1; /* invalid char */
584 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
585 i = i * 10;
586 if (i + j < i) return 1; /* check for addition overflow */
587 i = i + j;
588 }
589 } else {
590 /* negative number */
591 s++;
592 while (--len > 0) {
593 j = (*s++) - '0';
594 if (j > 9) return 1; /* invalid char */
595 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
596 i = i * 10;
597 if (i - j > i) return 1; /* check for subtract overflow */
598 i = i - j;
599 }
600 }
601 *ret = i;
602 return 0;
603}
604
605
606/* This function reads exactly <len> chars from <s> and converts them to a
607 * signed integer which it stores into <ret>. It accurately detects any error
608 * (truncated string, invalid chars, overflows). It is meant to be used in
609 * applications designed for hostile environments. It returns zero when the
610 * number has successfully been converted, non-zero otherwise. When an error
611 * is returned, the <ret> value is left untouched. It is about 3 times slower
612 * than str2irc().
613 */
614#ifndef LLONG_MAX
615#define LLONG_MAX 9223372036854775807LL
616#define LLONG_MIN (-LLONG_MAX - 1LL)
617#endif
618
619int strl2llrc(const char *s, int len, long long *ret)
620{
621 long long i = 0;
622 int j;
623
624 if (!len)
625 return 1;
626
627 if (*s != '-') {
628 /* positive number */
629 while (len-- > 0) {
630 j = (*s++) - '0';
631 if (j > 9) return 1; /* invalid char */
632 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
633 i = i * 10LL;
634 if (i + j < i) return 1; /* check for addition overflow */
635 i = i + j;
636 }
637 } else {
638 /* negative number */
639 s++;
640 while (--len > 0) {
641 j = (*s++) - '0';
642 if (j > 9) return 1; /* invalid char */
643 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
644 i = i * 10LL;
645 if (i - j > i) return 1; /* check for subtract overflow */
646 i = i - j;
647 }
648 }
649 *ret = i;
650 return 0;
651}
652
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100653/* This function parses a time value optionally followed by a unit suffix among
654 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
655 * expected by the caller. The computation does its best to avoid overflows.
656 * The value is returned in <ret> if everything is fine, and a NULL is returned
657 * by the function. In case of error, a pointer to the error is returned and
658 * <ret> is left untouched. Values are automatically rounded up when needed.
659 */
660const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
661{
662 unsigned imult, idiv;
663 unsigned omult, odiv;
664 unsigned value;
665
666 omult = odiv = 1;
667
668 switch (unit_flags & TIME_UNIT_MASK) {
669 case TIME_UNIT_US: omult = 1000000; break;
670 case TIME_UNIT_MS: omult = 1000; break;
671 case TIME_UNIT_S: break;
672 case TIME_UNIT_MIN: odiv = 60; break;
673 case TIME_UNIT_HOUR: odiv = 3600; break;
674 case TIME_UNIT_DAY: odiv = 86400; break;
675 default: break;
676 }
677
678 value = 0;
679
680 while (1) {
681 unsigned int j;
682
683 j = *text - '0';
684 if (j > 9)
685 break;
686 text++;
687 value *= 10;
688 value += j;
689 }
690
691 imult = idiv = 1;
692 switch (*text) {
693 case '\0': /* no unit = default unit */
694 imult = omult = idiv = odiv = 1;
695 break;
696 case 's': /* second = unscaled unit */
697 break;
698 case 'u': /* microsecond : "us" */
699 if (text[1] == 's') {
700 idiv = 1000000;
701 text++;
702 }
703 break;
704 case 'm': /* millisecond : "ms" or minute: "m" */
705 if (text[1] == 's') {
706 idiv = 1000;
707 text++;
708 } else
709 imult = 60;
710 break;
711 case 'h': /* hour : "h" */
712 imult = 3600;
713 break;
714 case 'd': /* day : "d" */
715 imult = 86400;
716 break;
717 default:
718 return text;
719 break;
720 }
721
722 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
723 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
724 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
725 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
726
727 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
728 *ret = value;
729 return NULL;
730}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100731
Willy Tarreau946ba592009-05-10 15:41:18 +0200732/* copies at most <n> characters from <src> and always terminates with '\0' */
733char *my_strndup(const char *src, int n)
734{
735 int len = 0;
736 char *ret;
737
738 while (len < n && src[len])
739 len++;
740
741 ret = (char *)malloc(len + 1);
742 if (!ret)
743 return ret;
744 memcpy(ret, src, len);
745 ret[len] = '\0';
746 return ret;
747}
748
Willy Tarreau482b00d2009-10-04 22:48:42 +0200749/* This function returns the first unused key greater than or equal to <key> in
750 * ID tree <root>. Zero is returned if no place is found.
751 */
752unsigned int get_next_id(struct eb_root *root, unsigned int key)
753{
754 struct eb32_node *used;
755
756 do {
757 used = eb32_lookup_ge(root, key);
758 if (!used || used->key > key)
759 return key; /* key is available */
760 key++;
761 } while (key);
762 return key;
763}
764
765
Willy Tarreaubaaee002006-06-26 02:48:02 +0200766/*
767 * Local variables:
768 * c-indent-level: 8
769 * c-basic-offset: 8
770 * End:
771 */