blob: 8aaa594dad6e8878d9e2063dd4e56865b4a6a135 [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
538/* This one is 7 times faster than strtol() on athlon with checks.
539 * It returns the value of the number composed of all valid digits read,
540 * and can process negative numbers too.
541 */
542int strl2ic(const char *s, int len)
543{
544 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200545 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100546
547 if (len > 0) {
548 if (*s != '-') {
549 /* positive number */
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 } else {
558 /* negative number */
559 s++;
560 while (--len > 0) {
561 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200562 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100563 if (j > 9)
564 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200565 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100566 }
567 }
568 }
569 return i;
570}
571
572
573/* This function reads exactly <len> chars from <s> and converts them to a
574 * signed integer which it stores into <ret>. It accurately detects any error
575 * (truncated string, invalid chars, overflows). It is meant to be used in
576 * applications designed for hostile environments. It returns zero when the
577 * number has successfully been converted, non-zero otherwise. When an error
578 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
579 * faster than strtol().
580 */
581int strl2irc(const char *s, int len, int *ret)
582{
583 int i = 0;
584 int j;
585
586 if (!len)
587 return 1;
588
589 if (*s != '-') {
590 /* positive number */
591 while (len-- > 0) {
592 j = (*s++) - '0';
593 if (j > 9) return 1; /* invalid char */
594 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
595 i = i * 10;
596 if (i + j < i) return 1; /* check for addition overflow */
597 i = i + j;
598 }
599 } else {
600 /* negative number */
601 s++;
602 while (--len > 0) {
603 j = (*s++) - '0';
604 if (j > 9) return 1; /* invalid char */
605 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
606 i = i * 10;
607 if (i - j > i) return 1; /* check for subtract overflow */
608 i = i - j;
609 }
610 }
611 *ret = i;
612 return 0;
613}
614
615
616/* This function reads exactly <len> chars from <s> and converts them to a
617 * signed integer which it stores into <ret>. It accurately detects any error
618 * (truncated string, invalid chars, overflows). It is meant to be used in
619 * applications designed for hostile environments. It returns zero when the
620 * number has successfully been converted, non-zero otherwise. When an error
621 * is returned, the <ret> value is left untouched. It is about 3 times slower
622 * than str2irc().
623 */
624#ifndef LLONG_MAX
625#define LLONG_MAX 9223372036854775807LL
626#define LLONG_MIN (-LLONG_MAX - 1LL)
627#endif
628
629int strl2llrc(const char *s, int len, long long *ret)
630{
631 long long i = 0;
632 int j;
633
634 if (!len)
635 return 1;
636
637 if (*s != '-') {
638 /* positive number */
639 while (len-- > 0) {
640 j = (*s++) - '0';
641 if (j > 9) return 1; /* invalid char */
642 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
643 i = i * 10LL;
644 if (i + j < i) return 1; /* check for addition overflow */
645 i = i + j;
646 }
647 } else {
648 /* negative number */
649 s++;
650 while (--len > 0) {
651 j = (*s++) - '0';
652 if (j > 9) return 1; /* invalid char */
653 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
654 i = i * 10LL;
655 if (i - j > i) return 1; /* check for subtract overflow */
656 i = i - j;
657 }
658 }
659 *ret = i;
660 return 0;
661}
662
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100663/* This function parses a time value optionally followed by a unit suffix among
664 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
665 * expected by the caller. The computation does its best to avoid overflows.
666 * The value is returned in <ret> if everything is fine, and a NULL is returned
667 * by the function. In case of error, a pointer to the error is returned and
668 * <ret> is left untouched. Values are automatically rounded up when needed.
669 */
670const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
671{
672 unsigned imult, idiv;
673 unsigned omult, odiv;
674 unsigned value;
675
676 omult = odiv = 1;
677
678 switch (unit_flags & TIME_UNIT_MASK) {
679 case TIME_UNIT_US: omult = 1000000; break;
680 case TIME_UNIT_MS: omult = 1000; break;
681 case TIME_UNIT_S: break;
682 case TIME_UNIT_MIN: odiv = 60; break;
683 case TIME_UNIT_HOUR: odiv = 3600; break;
684 case TIME_UNIT_DAY: odiv = 86400; break;
685 default: break;
686 }
687
688 value = 0;
689
690 while (1) {
691 unsigned int j;
692
693 j = *text - '0';
694 if (j > 9)
695 break;
696 text++;
697 value *= 10;
698 value += j;
699 }
700
701 imult = idiv = 1;
702 switch (*text) {
703 case '\0': /* no unit = default unit */
704 imult = omult = idiv = odiv = 1;
705 break;
706 case 's': /* second = unscaled unit */
707 break;
708 case 'u': /* microsecond : "us" */
709 if (text[1] == 's') {
710 idiv = 1000000;
711 text++;
712 }
713 break;
714 case 'm': /* millisecond : "ms" or minute: "m" */
715 if (text[1] == 's') {
716 idiv = 1000;
717 text++;
718 } else
719 imult = 60;
720 break;
721 case 'h': /* hour : "h" */
722 imult = 3600;
723 break;
724 case 'd': /* day : "d" */
725 imult = 86400;
726 break;
727 default:
728 return text;
729 break;
730 }
731
732 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
733 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
734 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
735 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
736
737 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
738 *ret = value;
739 return NULL;
740}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100741
Emeric Brun39132b22010-01-04 14:57:24 +0100742/* this function converts the string starting at <text> to an unsigned int
743 * stored in <ret>. If an error is detected, the pointer to the unexpected
744 * character is returned. If the conversio is succesful, NULL is returned.
745 */
746const char *parse_size_err(const char *text, unsigned *ret) {
747 unsigned value = 0;
748
749 while (1) {
750 unsigned int j;
751
752 j = *text - '0';
753 if (j > 9)
754 break;
755 if (value > ~0U / 10)
756 return text;
757 value *= 10;
758 if (value > (value + j))
759 return text;
760 value += j;
761 text++;
762 }
763
764 switch (*text) {
765 case '\0':
766 break;
767 case 'K':
768 case 'k':
769 if (value > ~0U >> 10)
770 return text;
771 value = value << 10;
772 break;
773 case 'M':
774 case 'm':
775 if (value > ~0U >> 20)
776 return text;
777 value = value << 20;
778 break;
779 case 'G':
780 case 'g':
781 if (value > ~0U >> 30)
782 return text;
783 value = value << 30;
784 break;
785 default:
786 return text;
787 }
788
789 *ret = value;
790 return NULL;
791}
792
Willy Tarreau946ba592009-05-10 15:41:18 +0200793/* copies at most <n> characters from <src> and always terminates with '\0' */
794char *my_strndup(const char *src, int n)
795{
796 int len = 0;
797 char *ret;
798
799 while (len < n && src[len])
800 len++;
801
802 ret = (char *)malloc(len + 1);
803 if (!ret)
804 return ret;
805 memcpy(ret, src, len);
806 ret[len] = '\0';
807 return ret;
808}
809
Willy Tarreau482b00d2009-10-04 22:48:42 +0200810/* This function returns the first unused key greater than or equal to <key> in
811 * ID tree <root>. Zero is returned if no place is found.
812 */
813unsigned int get_next_id(struct eb_root *root, unsigned int key)
814{
815 struct eb32_node *used;
816
817 do {
818 used = eb32_lookup_ge(root, key);
819 if (!used || used->key > key)
820 return key; /* key is available */
821 key++;
822 } while (key);
823 return key;
824}
825
Willy Tarreau348238b2010-01-18 15:05:57 +0100826/* This function compares a sample word possibly followed by blanks to another
827 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
828 * otherwise zero. This intends to be used when checking HTTP headers for some
829 * values. Note that it validates a word followed only by blanks but does not
830 * validate a word followed by blanks then other chars.
831 */
832int word_match(const char *sample, int slen, const char *word, int wlen)
833{
834 if (slen < wlen)
835 return 0;
836
837 while (wlen) {
838 char c = *sample ^ *word;
839 if (c && c != ('A' ^ 'a'))
840 return 0;
841 sample++;
842 word++;
843 slen--;
844 wlen--;
845 }
846
847 while (slen) {
848 if (*sample != ' ' && *sample != '\t')
849 return 0;
850 sample++;
851 slen--;
852 }
853 return 1;
854}
Willy Tarreau482b00d2009-10-04 22:48:42 +0200855
Willy Tarreaud54bbdc2009-09-07 11:00:31 +0200856/* Converts any text-formatted IPv4 address to a host-order IPv4 address. It
857 * is particularly fast because it avoids expensive operations such as
858 * multiplies, which are optimized away at the end. It requires a properly
859 * formated address though (3 points).
860 */
861unsigned int inetaddr_host(const char *text)
862{
863 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
864 register unsigned int dig100, dig10, dig1;
865 int s;
866 const char *p, *d;
867
868 dig1 = dig10 = dig100 = ascii_zero;
869 s = 24;
870
871 p = text;
872 while (1) {
873 if (((unsigned)(*p - '0')) <= 9) {
874 p++;
875 continue;
876 }
877
878 /* here, we have a complete byte between <text> and <p> (exclusive) */
879 if (p == text)
880 goto end;
881
882 d = p - 1;
883 dig1 |= (unsigned int)(*d << s);
884 if (d == text)
885 goto end;
886
887 d--;
888 dig10 |= (unsigned int)(*d << s);
889 if (d == text)
890 goto end;
891
892 d--;
893 dig100 |= (unsigned int)(*d << s);
894 end:
895 if (!s || *p != '.')
896 break;
897
898 s -= 8;
899 text = ++p;
900 }
901
902 dig100 -= ascii_zero;
903 dig10 -= ascii_zero;
904 dig1 -= ascii_zero;
905 return ((dig100 * 10) + dig10) * 10 + dig1;
906}
907
908/*
909 * Idem except the first unparsed character has to be passed in <stop>.
910 */
911unsigned int inetaddr_host_lim(const char *text, const char *stop)
912{
913 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
914 register unsigned int dig100, dig10, dig1;
915 int s;
916 const char *p, *d;
917
918 dig1 = dig10 = dig100 = ascii_zero;
919 s = 24;
920
921 p = text;
922 while (1) {
923 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
924 p++;
925 continue;
926 }
927
928 /* here, we have a complete byte between <text> and <p> (exclusive) */
929 if (p == text)
930 goto end;
931
932 d = p - 1;
933 dig1 |= (unsigned int)(*d << s);
934 if (d == text)
935 goto end;
936
937 d--;
938 dig10 |= (unsigned int)(*d << s);
939 if (d == text)
940 goto end;
941
942 d--;
943 dig100 |= (unsigned int)(*d << s);
944 end:
945 if (!s || p == stop || *p != '.')
946 break;
947
948 s -= 8;
949 text = ++p;
950 }
951
952 dig100 -= ascii_zero;
953 dig10 -= ascii_zero;
954 dig1 -= ascii_zero;
955 return ((dig100 * 10) + dig10) * 10 + dig1;
956}
957
958/*
959 * Idem except the pointer to first unparsed byte is returned into <ret> which
960 * must not be NULL.
961 */
962unsigned int inetaddr_host_lim_ret(const char *text, char *stop, const char **ret)
963{
964 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
965 register unsigned int dig100, dig10, dig1;
966 int s;
967 const char *p, *d;
968
969 dig1 = dig10 = dig100 = ascii_zero;
970 s = 24;
971
972 p = text;
973 while (1) {
974 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
975 p++;
976 continue;
977 }
978
979 /* here, we have a complete byte between <text> and <p> (exclusive) */
980 if (p == text)
981 goto end;
982
983 d = p - 1;
984 dig1 |= (unsigned int)(*d << s);
985 if (d == text)
986 goto end;
987
988 d--;
989 dig10 |= (unsigned int)(*d << s);
990 if (d == text)
991 goto end;
992
993 d--;
994 dig100 |= (unsigned int)(*d << s);
995 end:
996 if (!s || p == stop || *p != '.')
997 break;
998
999 s -= 8;
1000 text = ++p;
1001 }
1002
1003 *ret = p;
1004 dig100 -= ascii_zero;
1005 dig10 -= ascii_zero;
1006 dig1 -= ascii_zero;
1007 return ((dig100 * 10) + dig10) * 10 + dig1;
1008}
1009
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001010/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
1011 * or the number of chars read in case of success. Maybe this could be replaced
1012 * by one of the functions above. Also, apparently this function does not support
1013 * hosts above 255 and requires exactly 4 octets.
1014 */
1015int buf2ip(const char *buf, size_t len, struct in_addr *dst)
1016{
1017 const char *addr;
1018 int saw_digit, octets, ch;
1019 u_char tmp[4], *tp;
1020 const char *cp = buf;
1021
1022 saw_digit = 0;
1023 octets = 0;
1024 *(tp = tmp) = 0;
1025
1026 for (addr = buf; addr - buf < len; addr++) {
1027 unsigned char digit = (ch = *addr) - '0';
1028
1029 if (digit > 9 && ch != '.')
1030 break;
1031
1032 if (digit <= 9) {
1033 u_int new = *tp * 10 + digit;
1034
1035 if (new > 255)
1036 return 0;
1037
1038 *tp = new;
1039
1040 if (!saw_digit) {
1041 if (++octets > 4)
1042 return 0;
1043 saw_digit = 1;
1044 }
1045 } else if (ch == '.' && saw_digit) {
1046 if (octets == 4)
1047 return 0;
1048
1049 *++tp = 0;
1050 saw_digit = 0;
1051 } else
1052 return 0;
1053 }
1054
1055 if (octets < 4)
1056 return 0;
1057
1058 memcpy(&dst->s_addr, tmp, 4);
1059 return addr - cp;
1060}
1061
Willy Tarreauacf95772010-06-14 19:09:21 +02001062/* To be used to quote config arg positions. Returns the short string at <ptr>
1063 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
1064 * if ptr is NULL or empty. The string is locally allocated.
1065 */
1066const char *quote_arg(const char *ptr)
1067{
1068 static char val[32];
1069 int i;
1070
1071 if (!ptr || !*ptr)
1072 return "end of line";
1073 val[0] = '\'';
1074 for (i = 1; i < sizeof(val) - 1 && *ptr; i++)
1075 val[i] = *ptr++;
1076 val[i++] = '\'';
1077 val[i] = '\0';
1078 return val;
1079}
1080
Willy Tarreau5b180202010-07-18 10:40:48 +02001081/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
1082int get_std_op(const char *str)
1083{
1084 int ret = -1;
1085
1086 if (*str == 'e' && str[1] == 'q')
1087 ret = STD_OP_EQ;
1088 else if (*str == 'n' && str[1] == 'e')
1089 ret = STD_OP_NE;
1090 else if (*str == 'l') {
1091 if (str[1] == 'e') ret = STD_OP_LE;
1092 else if (str[1] == 't') ret = STD_OP_LT;
1093 }
1094 else if (*str == 'g') {
1095 if (str[1] == 'e') ret = STD_OP_GE;
1096 else if (str[1] == 't') ret = STD_OP_GT;
1097 }
1098
1099 if (ret == -1 || str[2] != '\0')
1100 return -1;
1101 return ret;
1102}
1103
Willy Tarreaubaaee002006-06-26 02:48:02 +02001104/*
1105 * Local variables:
1106 * c-indent-level: 8
1107 * c-basic-offset: 8
1108 * End:
1109 */