blob: 60f5af1f5d5e5c53d199a9e99a8dba7a562fda4d [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General purpose functions.
3 *
Willy Tarreau348238b2010-01-18 15:05:57 +01004 * Copyright 2000-2010 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 */
Emeric Brun3a7fce52010-01-04 14:54:38 +010060char *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.
Willy Tarreaud5191e72010-02-09 20:50:45 +0100122 * NULL is returned if the socket path is invalid (too long).
Robert Tsai81ae1952007-12-05 10:47:29 +0100123 */
Willy Tarreaucaf720d2008-03-07 10:07:04 +0100124struct sockaddr_un *str2sun(const char *str)
Robert Tsai81ae1952007-12-05 10:47:29 +0100125{
Willy Tarreau127f9662007-12-06 00:53:51 +0100126 static struct sockaddr_un su;
Robert Tsai81ae1952007-12-05 10:47:29 +0100127 int strsz; /* length included null */
128
Willy Tarreau127f9662007-12-06 00:53:51 +0100129 memset(&su, 0, sizeof(su));
Robert Tsai81ae1952007-12-05 10:47:29 +0100130 strsz = strlen(str) + 1;
Willy Tarreau127f9662007-12-06 00:53:51 +0100131 if (strsz > sizeof(su.sun_path)) {
Willy Tarreaud5191e72010-02-09 20:50:45 +0100132 return NULL;
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 Tarreau88e05812010-03-03 00:16:00 +0100187 if (!isalnum((int)(unsigned char)*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) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100206 if (!isalnum((int)(unsigned char)*name) && *name != '.' &&
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200207 *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,
Willy Tarreaud5191e72010-02-09 20:50:45 +0100219 * a host name, or empty or "*" to indicate INADDR_ANY. NULL is returned
220 * if the host part cannot be resolved.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200221 */
222struct sockaddr_in *str2sa(char *str)
223{
224 static struct sockaddr_in sa;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100225 struct sockaddr_in *ret = NULL;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200226 char *c;
227 int port;
228
229 memset(&sa, 0, sizeof(sa));
230 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200231 if (str == NULL)
Willy Tarreaud5191e72010-02-09 20:50:45 +0100232 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200233
234 if ((c = strrchr(str,':')) != NULL) {
235 *c++ = '\0';
236 port = atol(c);
237 }
238 else
239 port = 0;
240
241 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
242 sa.sin_addr.s_addr = INADDR_ANY;
243 }
244 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
Willy Tarreaud5191e72010-02-09 20:50:45 +0100245 struct hostent *he = gethostbyname(str);
246 if (!he)
247 goto out;
248 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200249 }
250 sa.sin_port = htons(port);
251 sa.sin_family = AF_INET;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100252 ret = &sa;
253 out:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200254 free(str);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100255 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200256}
257
258/*
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200259 * converts <str> to a struct sockaddr_in* which is locally allocated, and a
260 * port range consisting in two integers. The low and high end are always set
261 * even if the port is unspecified, in which case (0,0) is returned. The low
262 * port is set in the sockaddr_in. Thus, it is enough to check the size of the
263 * returned range to know if an array must be allocated or not. The format is
264 * "addr[:port[-port]]", where "addr" can be a dotted IPv4 address, a host
Willy Tarreaud5191e72010-02-09 20:50:45 +0100265 * name, or empty or "*" to indicate INADDR_ANY. NULL is returned if the host
266 * part cannot be resolved.
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200267 */
268struct sockaddr_in *str2sa_range(char *str, int *low, int *high)
269{
270 static struct sockaddr_in sa;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100271 struct sockaddr_in *ret = NULL;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200272 char *c;
273 int portl, porth;
274
275 memset(&sa, 0, sizeof(sa));
276 str = strdup(str);
277 if (str == NULL)
Willy Tarreaud5191e72010-02-09 20:50:45 +0100278 goto out;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200279
280 if ((c = strrchr(str,':')) != NULL) {
281 char *sep;
282 *c++ = '\0';
283 sep = strchr(c, '-');
284 if (sep)
285 *sep++ = '\0';
286 else
287 sep = c;
288 portl = atol(c);
289 porth = atol(sep);
290 }
291 else {
292 portl = 0;
293 porth = 0;
294 }
295
296 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
297 sa.sin_addr.s_addr = INADDR_ANY;
298 }
299 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
Willy Tarreaud5191e72010-02-09 20:50:45 +0100300 struct hostent *he = gethostbyname(str);
301 if (!he)
302 goto out;
303 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200304 }
305 sa.sin_port = htons(portl);
306 sa.sin_family = AF_INET;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100307 ret = &sa;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200308
309 *low = portl;
310 *high = porth;
311
Willy Tarreaud5191e72010-02-09 20:50:45 +0100312 out:
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200313 free(str);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100314 return ret;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200315}
316
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100317/* converts <str> to a struct in_addr containing a network mask. It can be
318 * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1
319 * if the conversion succeeds otherwise non-zero.
320 */
321int str2mask(const char *str, struct in_addr *mask)
322{
323 if (strchr(str, '.') != NULL) { /* dotted notation */
324 if (!inet_pton(AF_INET, str, mask))
325 return 0;
326 }
327 else { /* mask length */
328 char *err;
329 unsigned long len = strtol(str, &err, 10);
330
331 if (!*str || (err && *err) || (unsigned)len > 32)
332 return 0;
333 if (len)
334 mask->s_addr = htonl(~0UL << (32 - len));
335 else
336 mask->s_addr = 0;
337 }
338 return 1;
339}
340
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200341/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200342 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200343 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
344 * is optionnal and either in the dotted or CIDR notation.
345 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
346 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200347int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200348{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200349 __label__ out_free, out_err;
350 char *c, *s;
351 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200352
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200353 s = strdup(str);
354 if (!s)
355 return 0;
356
Willy Tarreaubaaee002006-06-26 02:48:02 +0200357 memset(mask, 0, sizeof(*mask));
358 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200359
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200360 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361 *c++ = '\0';
362 /* c points to the mask */
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100363 if (!str2mask(c, mask))
364 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200365 }
366 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100367 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200368 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200369 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200370 struct hostent *he;
371
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200372 if ((he = gethostbyname(s)) == NULL) {
373 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200374 }
375 else
376 *addr = *(struct in_addr *) *(he->h_addr_list);
377 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200378
379 ret_val = 1;
380 out_free:
381 free(s);
382 return ret_val;
383 out_err:
384 ret_val = 0;
385 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386}
387
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100388
389/*
390 * Parse IP address found in url.
391 */
Willy Tarreau106f9792009-09-19 07:54:16 +0200392int url2ip(const char *addr, struct in_addr *dst)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100393{
394 int saw_digit, octets, ch;
395 u_char tmp[4], *tp;
396 const char *cp = addr;
397
398 saw_digit = 0;
399 octets = 0;
400 *(tp = tmp) = 0;
401
402 while (*addr) {
403 unsigned char digit = (ch = *addr++) - '0';
404 if (digit > 9 && ch != '.')
405 break;
406 if (digit <= 9) {
407 u_int new = *tp * 10 + digit;
408 if (new > 255)
409 return 0;
410 *tp = new;
411 if (!saw_digit) {
412 if (++octets > 4)
413 return 0;
414 saw_digit = 1;
415 }
416 } else if (ch == '.' && saw_digit) {
417 if (octets == 4)
418 return 0;
419 *++tp = 0;
420 saw_digit = 0;
421 } else
422 return 0;
423 }
424
425 if (octets < 4)
426 return 0;
427
428 memcpy(&dst->s_addr, tmp, 4);
429 return addr-cp-1;
430}
431
432/*
433 * Resolve destination server from URL. Convert <str> to a sockaddr_in*.
434 */
435int url2sa(const char *url, int ulen, struct sockaddr_in *addr)
436{
437 const char *curr = url, *cp = url;
438 int ret, url_code = 0;
439 unsigned int http_code = 0;
440
441 /* Cleanup the room */
442 addr->sin_family = AF_INET;
443 addr->sin_addr.s_addr = 0;
444 addr->sin_port = 0;
445
446 /* Firstly, try to find :// pattern */
447 while (curr < url+ulen && url_code != 0x3a2f2f) {
448 url_code = ((url_code & 0xffff) << 8);
449 url_code += (unsigned char)*curr++;
450 }
451
452 /* Secondly, if :// pattern is found, verify parsed stuff
453 * before pattern is matching our http pattern.
454 * If so parse ip address and port in uri.
455 *
456 * WARNING: Current code doesn't support dynamic async dns resolver.
457 */
458 if (url_code == 0x3a2f2f) {
459 while (cp < curr - 3)
460 http_code = (http_code << 8) + *cp++;
461 http_code |= 0x20202020; /* Turn everything to lower case */
462
463 /* HTTP url matching */
464 if (http_code == 0x68747470) {
465 /* We are looking for IP address. If you want to parse and
466 * resolve hostname found in url, you can use str2sa(), but
467 * be warned this can slow down global daemon performances
468 * while handling lagging dns responses.
469 */
470 ret = url2ip(curr, &addr->sin_addr);
471 if (!ret)
472 return -1;
473 curr += ret;
Willy Tarreaud1cd2762007-12-02 10:55:56 +0100474 addr->sin_port = (*curr == ':') ? str2uic(++curr) : 80;
475 addr->sin_port = htons(addr->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100476 }
477 return 0;
478 }
479
480 return -1;
481}
482
Willy Tarreaubaaee002006-06-26 02:48:02 +0200483/* will try to encode the string <string> replacing all characters tagged in
484 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
485 * prefixed by <escape>, and will store the result between <start> (included)
486 * and <stop> (excluded), and will always terminate the string with a '\0'
487 * before <stop>. The position of the '\0' is returned if the conversion
488 * completes. If bytes are missing between <start> and <stop>, then the
489 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
490 * cannot even be stored so we return <start> without writing the 0.
491 * The input string must also be zero-terminated.
492 */
493const char hextab[16] = "0123456789ABCDEF";
494char *encode_string(char *start, char *stop,
495 const char escape, const fd_set *map,
496 const char *string)
497{
498 if (start < stop) {
499 stop--; /* reserve one byte for the final '\0' */
500 while (start < stop && *string != '\0') {
501 if (!FD_ISSET((unsigned char)(*string), map))
502 *start++ = *string;
503 else {
504 if (start + 3 >= stop)
505 break;
506 *start++ = escape;
507 *start++ = hextab[(*string >> 4) & 15];
508 *start++ = hextab[*string & 15];
509 }
510 string++;
511 }
512 *start = '\0';
513 }
514 return start;
515}
516
517
Willy Tarreau6911fa42007-03-04 18:06:08 +0100518unsigned int str2ui(const char *s)
519{
520 return __str2ui(s);
521}
522
523unsigned int str2uic(const char *s)
524{
525 return __str2uic(s);
526}
527
528unsigned int strl2ui(const char *s, int len)
529{
530 return __strl2ui(s, len);
531}
532
533unsigned int strl2uic(const char *s, int len)
534{
535 return __strl2uic(s, len);
536}
537
Willy Tarreau4ec83cd2010-10-15 23:19:55 +0200538unsigned int read_uint(const char **s, const char *end)
539{
540 return __read_uint(s, end);
541}
542
Willy Tarreau6911fa42007-03-04 18:06:08 +0100543/* This one is 7 times faster than strtol() on athlon with checks.
544 * It returns the value of the number composed of all valid digits read,
545 * and can process negative numbers too.
546 */
547int strl2ic(const char *s, int len)
548{
549 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200550 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100551
552 if (len > 0) {
553 if (*s != '-') {
554 /* positive number */
555 while (len-- > 0) {
556 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200557 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100558 if (j > 9)
559 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200560 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100561 }
562 } else {
563 /* negative number */
564 s++;
565 while (--len > 0) {
566 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200567 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100568 if (j > 9)
569 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200570 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100571 }
572 }
573 }
574 return i;
575}
576
577
578/* This function reads exactly <len> chars from <s> and converts them to a
579 * signed integer which it stores into <ret>. It accurately detects any error
580 * (truncated string, invalid chars, overflows). It is meant to be used in
581 * applications designed for hostile environments. It returns zero when the
582 * number has successfully been converted, non-zero otherwise. When an error
583 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
584 * faster than strtol().
585 */
586int strl2irc(const char *s, int len, int *ret)
587{
588 int i = 0;
589 int j;
590
591 if (!len)
592 return 1;
593
594 if (*s != '-') {
595 /* positive number */
596 while (len-- > 0) {
597 j = (*s++) - '0';
598 if (j > 9) return 1; /* invalid char */
599 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
600 i = i * 10;
601 if (i + j < i) return 1; /* check for addition overflow */
602 i = i + j;
603 }
604 } else {
605 /* negative number */
606 s++;
607 while (--len > 0) {
608 j = (*s++) - '0';
609 if (j > 9) return 1; /* invalid char */
610 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
611 i = i * 10;
612 if (i - j > i) return 1; /* check for subtract overflow */
613 i = i - j;
614 }
615 }
616 *ret = i;
617 return 0;
618}
619
620
621/* This function reads exactly <len> chars from <s> and converts them to a
622 * signed integer which it stores into <ret>. It accurately detects any error
623 * (truncated string, invalid chars, overflows). It is meant to be used in
624 * applications designed for hostile environments. It returns zero when the
625 * number has successfully been converted, non-zero otherwise. When an error
626 * is returned, the <ret> value is left untouched. It is about 3 times slower
627 * than str2irc().
628 */
629#ifndef LLONG_MAX
630#define LLONG_MAX 9223372036854775807LL
631#define LLONG_MIN (-LLONG_MAX - 1LL)
632#endif
633
634int strl2llrc(const char *s, int len, long long *ret)
635{
636 long long i = 0;
637 int j;
638
639 if (!len)
640 return 1;
641
642 if (*s != '-') {
643 /* positive number */
644 while (len-- > 0) {
645 j = (*s++) - '0';
646 if (j > 9) return 1; /* invalid char */
647 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
648 i = i * 10LL;
649 if (i + j < i) return 1; /* check for addition overflow */
650 i = i + j;
651 }
652 } else {
653 /* negative number */
654 s++;
655 while (--len > 0) {
656 j = (*s++) - '0';
657 if (j > 9) return 1; /* invalid char */
658 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
659 i = i * 10LL;
660 if (i - j > i) return 1; /* check for subtract overflow */
661 i = i - j;
662 }
663 }
664 *ret = i;
665 return 0;
666}
667
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100668/* This function parses a time value optionally followed by a unit suffix among
669 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
670 * expected by the caller. The computation does its best to avoid overflows.
671 * The value is returned in <ret> if everything is fine, and a NULL is returned
672 * by the function. In case of error, a pointer to the error is returned and
673 * <ret> is left untouched. Values are automatically rounded up when needed.
674 */
675const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
676{
677 unsigned imult, idiv;
678 unsigned omult, odiv;
679 unsigned value;
680
681 omult = odiv = 1;
682
683 switch (unit_flags & TIME_UNIT_MASK) {
684 case TIME_UNIT_US: omult = 1000000; break;
685 case TIME_UNIT_MS: omult = 1000; break;
686 case TIME_UNIT_S: break;
687 case TIME_UNIT_MIN: odiv = 60; break;
688 case TIME_UNIT_HOUR: odiv = 3600; break;
689 case TIME_UNIT_DAY: odiv = 86400; break;
690 default: break;
691 }
692
693 value = 0;
694
695 while (1) {
696 unsigned int j;
697
698 j = *text - '0';
699 if (j > 9)
700 break;
701 text++;
702 value *= 10;
703 value += j;
704 }
705
706 imult = idiv = 1;
707 switch (*text) {
708 case '\0': /* no unit = default unit */
709 imult = omult = idiv = odiv = 1;
710 break;
711 case 's': /* second = unscaled unit */
712 break;
713 case 'u': /* microsecond : "us" */
714 if (text[1] == 's') {
715 idiv = 1000000;
716 text++;
717 }
718 break;
719 case 'm': /* millisecond : "ms" or minute: "m" */
720 if (text[1] == 's') {
721 idiv = 1000;
722 text++;
723 } else
724 imult = 60;
725 break;
726 case 'h': /* hour : "h" */
727 imult = 3600;
728 break;
729 case 'd': /* day : "d" */
730 imult = 86400;
731 break;
732 default:
733 return text;
734 break;
735 }
736
737 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
738 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
739 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
740 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
741
742 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
743 *ret = value;
744 return NULL;
745}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100746
Emeric Brun39132b22010-01-04 14:57:24 +0100747/* this function converts the string starting at <text> to an unsigned int
748 * stored in <ret>. If an error is detected, the pointer to the unexpected
749 * character is returned. If the conversio is succesful, NULL is returned.
750 */
751const char *parse_size_err(const char *text, unsigned *ret) {
752 unsigned value = 0;
753
754 while (1) {
755 unsigned int j;
756
757 j = *text - '0';
758 if (j > 9)
759 break;
760 if (value > ~0U / 10)
761 return text;
762 value *= 10;
763 if (value > (value + j))
764 return text;
765 value += j;
766 text++;
767 }
768
769 switch (*text) {
770 case '\0':
771 break;
772 case 'K':
773 case 'k':
774 if (value > ~0U >> 10)
775 return text;
776 value = value << 10;
777 break;
778 case 'M':
779 case 'm':
780 if (value > ~0U >> 20)
781 return text;
782 value = value << 20;
783 break;
784 case 'G':
785 case 'g':
786 if (value > ~0U >> 30)
787 return text;
788 value = value << 30;
789 break;
790 default:
791 return text;
792 }
793
794 *ret = value;
795 return NULL;
796}
797
Willy Tarreau946ba592009-05-10 15:41:18 +0200798/* copies at most <n> characters from <src> and always terminates with '\0' */
799char *my_strndup(const char *src, int n)
800{
801 int len = 0;
802 char *ret;
803
804 while (len < n && src[len])
805 len++;
806
807 ret = (char *)malloc(len + 1);
808 if (!ret)
809 return ret;
810 memcpy(ret, src, len);
811 ret[len] = '\0';
812 return ret;
813}
814
Willy Tarreau482b00d2009-10-04 22:48:42 +0200815/* This function returns the first unused key greater than or equal to <key> in
816 * ID tree <root>. Zero is returned if no place is found.
817 */
818unsigned int get_next_id(struct eb_root *root, unsigned int key)
819{
820 struct eb32_node *used;
821
822 do {
823 used = eb32_lookup_ge(root, key);
824 if (!used || used->key > key)
825 return key; /* key is available */
826 key++;
827 } while (key);
828 return key;
829}
830
Willy Tarreau348238b2010-01-18 15:05:57 +0100831/* This function compares a sample word possibly followed by blanks to another
832 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
833 * otherwise zero. This intends to be used when checking HTTP headers for some
834 * values. Note that it validates a word followed only by blanks but does not
835 * validate a word followed by blanks then other chars.
836 */
837int word_match(const char *sample, int slen, const char *word, int wlen)
838{
839 if (slen < wlen)
840 return 0;
841
842 while (wlen) {
843 char c = *sample ^ *word;
844 if (c && c != ('A' ^ 'a'))
845 return 0;
846 sample++;
847 word++;
848 slen--;
849 wlen--;
850 }
851
852 while (slen) {
853 if (*sample != ' ' && *sample != '\t')
854 return 0;
855 sample++;
856 slen--;
857 }
858 return 1;
859}
Willy Tarreau482b00d2009-10-04 22:48:42 +0200860
Willy Tarreaud54bbdc2009-09-07 11:00:31 +0200861/* Converts any text-formatted IPv4 address to a host-order IPv4 address. It
862 * is particularly fast because it avoids expensive operations such as
863 * multiplies, which are optimized away at the end. It requires a properly
864 * formated address though (3 points).
865 */
866unsigned int inetaddr_host(const char *text)
867{
868 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
869 register unsigned int dig100, dig10, dig1;
870 int s;
871 const char *p, *d;
872
873 dig1 = dig10 = dig100 = ascii_zero;
874 s = 24;
875
876 p = text;
877 while (1) {
878 if (((unsigned)(*p - '0')) <= 9) {
879 p++;
880 continue;
881 }
882
883 /* here, we have a complete byte between <text> and <p> (exclusive) */
884 if (p == text)
885 goto end;
886
887 d = p - 1;
888 dig1 |= (unsigned int)(*d << s);
889 if (d == text)
890 goto end;
891
892 d--;
893 dig10 |= (unsigned int)(*d << s);
894 if (d == text)
895 goto end;
896
897 d--;
898 dig100 |= (unsigned int)(*d << s);
899 end:
900 if (!s || *p != '.')
901 break;
902
903 s -= 8;
904 text = ++p;
905 }
906
907 dig100 -= ascii_zero;
908 dig10 -= ascii_zero;
909 dig1 -= ascii_zero;
910 return ((dig100 * 10) + dig10) * 10 + dig1;
911}
912
913/*
914 * Idem except the first unparsed character has to be passed in <stop>.
915 */
916unsigned int inetaddr_host_lim(const char *text, const char *stop)
917{
918 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
919 register unsigned int dig100, dig10, dig1;
920 int s;
921 const char *p, *d;
922
923 dig1 = dig10 = dig100 = ascii_zero;
924 s = 24;
925
926 p = text;
927 while (1) {
928 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
929 p++;
930 continue;
931 }
932
933 /* here, we have a complete byte between <text> and <p> (exclusive) */
934 if (p == text)
935 goto end;
936
937 d = p - 1;
938 dig1 |= (unsigned int)(*d << s);
939 if (d == text)
940 goto end;
941
942 d--;
943 dig10 |= (unsigned int)(*d << s);
944 if (d == text)
945 goto end;
946
947 d--;
948 dig100 |= (unsigned int)(*d << s);
949 end:
950 if (!s || p == stop || *p != '.')
951 break;
952
953 s -= 8;
954 text = ++p;
955 }
956
957 dig100 -= ascii_zero;
958 dig10 -= ascii_zero;
959 dig1 -= ascii_zero;
960 return ((dig100 * 10) + dig10) * 10 + dig1;
961}
962
963/*
964 * Idem except the pointer to first unparsed byte is returned into <ret> which
965 * must not be NULL.
966 */
Willy Tarreau74172752010-10-15 23:21:42 +0200967unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret)
Willy Tarreaud54bbdc2009-09-07 11:00:31 +0200968{
969 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
970 register unsigned int dig100, dig10, dig1;
971 int s;
Willy Tarreau74172752010-10-15 23:21:42 +0200972 char *p, *d;
Willy Tarreaud54bbdc2009-09-07 11:00:31 +0200973
974 dig1 = dig10 = dig100 = ascii_zero;
975 s = 24;
976
977 p = text;
978 while (1) {
979 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
980 p++;
981 continue;
982 }
983
984 /* here, we have a complete byte between <text> and <p> (exclusive) */
985 if (p == text)
986 goto end;
987
988 d = p - 1;
989 dig1 |= (unsigned int)(*d << s);
990 if (d == text)
991 goto end;
992
993 d--;
994 dig10 |= (unsigned int)(*d << s);
995 if (d == text)
996 goto end;
997
998 d--;
999 dig100 |= (unsigned int)(*d << s);
1000 end:
1001 if (!s || p == stop || *p != '.')
1002 break;
1003
1004 s -= 8;
1005 text = ++p;
1006 }
1007
1008 *ret = p;
1009 dig100 -= ascii_zero;
1010 dig10 -= ascii_zero;
1011 dig1 -= ascii_zero;
1012 return ((dig100 * 10) + dig10) * 10 + dig1;
1013}
1014
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001015/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
1016 * or the number of chars read in case of success. Maybe this could be replaced
1017 * by one of the functions above. Also, apparently this function does not support
1018 * hosts above 255 and requires exactly 4 octets.
1019 */
1020int buf2ip(const char *buf, size_t len, struct in_addr *dst)
1021{
1022 const char *addr;
1023 int saw_digit, octets, ch;
1024 u_char tmp[4], *tp;
1025 const char *cp = buf;
1026
1027 saw_digit = 0;
1028 octets = 0;
1029 *(tp = tmp) = 0;
1030
1031 for (addr = buf; addr - buf < len; addr++) {
1032 unsigned char digit = (ch = *addr) - '0';
1033
1034 if (digit > 9 && ch != '.')
1035 break;
1036
1037 if (digit <= 9) {
1038 u_int new = *tp * 10 + digit;
1039
1040 if (new > 255)
1041 return 0;
1042
1043 *tp = new;
1044
1045 if (!saw_digit) {
1046 if (++octets > 4)
1047 return 0;
1048 saw_digit = 1;
1049 }
1050 } else if (ch == '.' && saw_digit) {
1051 if (octets == 4)
1052 return 0;
1053
1054 *++tp = 0;
1055 saw_digit = 0;
1056 } else
1057 return 0;
1058 }
1059
1060 if (octets < 4)
1061 return 0;
1062
1063 memcpy(&dst->s_addr, tmp, 4);
1064 return addr - cp;
1065}
1066
Willy Tarreauacf95772010-06-14 19:09:21 +02001067/* To be used to quote config arg positions. Returns the short string at <ptr>
1068 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
1069 * if ptr is NULL or empty. The string is locally allocated.
1070 */
1071const char *quote_arg(const char *ptr)
1072{
1073 static char val[32];
1074 int i;
1075
1076 if (!ptr || !*ptr)
1077 return "end of line";
1078 val[0] = '\'';
1079 for (i = 1; i < sizeof(val) - 1 && *ptr; i++)
1080 val[i] = *ptr++;
1081 val[i++] = '\'';
1082 val[i] = '\0';
1083 return val;
1084}
1085
Willy Tarreau5b180202010-07-18 10:40:48 +02001086/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
1087int get_std_op(const char *str)
1088{
1089 int ret = -1;
1090
1091 if (*str == 'e' && str[1] == 'q')
1092 ret = STD_OP_EQ;
1093 else if (*str == 'n' && str[1] == 'e')
1094 ret = STD_OP_NE;
1095 else if (*str == 'l') {
1096 if (str[1] == 'e') ret = STD_OP_LE;
1097 else if (str[1] == 't') ret = STD_OP_LT;
1098 }
1099 else if (*str == 'g') {
1100 if (str[1] == 'e') ret = STD_OP_GE;
1101 else if (str[1] == 't') ret = STD_OP_GT;
1102 }
1103
1104 if (ret == -1 || str[2] != '\0')
1105 return -1;
1106 return ret;
1107}
1108
Willy Tarreaubaaee002006-06-26 02:48:02 +02001109/*
1110 * Local variables:
1111 * c-indent-level: 8
1112 * c-basic-offset: 8
1113 * End:
1114 */