blob: 27fa374eac68d255934fd952a981e30fd5268cb2 [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 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100222struct sockaddr_storage *str2sa(char *str)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200223{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100224 static struct sockaddr_storage sa;
225 struct sockaddr_storage *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
David du Colombier6f5ccb12011-03-10 22:26:24 +0100241 sa.ss_family = AF_INET;
242 ((struct sockaddr_in *)&sa)->sin_port = htons(port);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200243 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100244 ((struct sockaddr_in *)&sa)->sin_addr.s_addr = INADDR_ANY;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200245 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100246 else if (!inet_pton(sa.ss_family, str, &((struct sockaddr_in *)&sa)->sin_addr)) {
Willy Tarreaud5191e72010-02-09 20:50:45 +0100247 struct hostent *he = gethostbyname(str);
248 if (!he)
249 goto out;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100250 ((struct sockaddr_in *)&sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
Willy Tarreaubaaee002006-06-26 02:48:02 +0200251 }
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 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100268struct sockaddr_storage *str2sa_range(char *str, int *low, int *high)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200269{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100270 static struct sockaddr_storage sa;
271 struct sockaddr_storage *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
David du Colombier6f5ccb12011-03-10 22:26:24 +0100296 sa.ss_family = AF_INET;
297 ((struct sockaddr_in *)&sa)->sin_port = htonl(portl);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200298 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100299 ((struct sockaddr_in *)&sa)->sin_addr.s_addr = INADDR_ANY;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200300 }
David du Colombier6f5ccb12011-03-10 22:26:24 +0100301 else if (!inet_pton(sa.ss_family, str, &((struct sockaddr_in *)&sa)->sin_addr)) {
Willy Tarreaud5191e72010-02-09 20:50:45 +0100302 struct hostent *he = gethostbyname(str);
303 if (!he)
304 goto out;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100305 ((struct sockaddr_in *)&sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200306 }
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/*
David du Colombier6f5ccb12011-03-10 22:26:24 +0100390 * Parse IPv4 address found in url.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100391 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100392int url2ipv4(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/*
David du Colombier6f5ccb12011-03-10 22:26:24 +0100433 * Resolve destination server from URL. Convert <str> to a sockaddr_storage*.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100434 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100435int url2sa(const char *url, int ulen, struct sockaddr_storage *addr)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100436{
437 const char *curr = url, *cp = url;
438 int ret, url_code = 0;
439 unsigned int http_code = 0;
440
441 /* Cleanup the room */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100442
443 /* FIXME: assume IPv4 only for now */
444 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
445 ((struct sockaddr_in *)addr)->sin_addr.s_addr = 0;
446 ((struct sockaddr_in *)addr)->sin_port = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100447
448 /* Firstly, try to find :// pattern */
449 while (curr < url+ulen && url_code != 0x3a2f2f) {
450 url_code = ((url_code & 0xffff) << 8);
451 url_code += (unsigned char)*curr++;
452 }
453
454 /* Secondly, if :// pattern is found, verify parsed stuff
455 * before pattern is matching our http pattern.
456 * If so parse ip address and port in uri.
457 *
458 * WARNING: Current code doesn't support dynamic async dns resolver.
459 */
460 if (url_code == 0x3a2f2f) {
461 while (cp < curr - 3)
462 http_code = (http_code << 8) + *cp++;
463 http_code |= 0x20202020; /* Turn everything to lower case */
464
465 /* HTTP url matching */
466 if (http_code == 0x68747470) {
467 /* We are looking for IP address. If you want to parse and
468 * resolve hostname found in url, you can use str2sa(), but
469 * be warned this can slow down global daemon performances
470 * while handling lagging dns responses.
471 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100472 ret = url2ipv4(curr, &((struct sockaddr_in *)&addr)->sin_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100473 if (!ret)
474 return -1;
475 curr += ret;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100476 ((struct sockaddr_in *)addr)->sin_port = (*curr == ':') ? str2uic(++curr) : 80;
477 ((struct sockaddr_in *)addr)->sin_port = htons(((struct sockaddr_in *)&addr)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100478 }
479 return 0;
480 }
481
482 return -1;
483}
484
Willy Tarreaubaaee002006-06-26 02:48:02 +0200485/* will try to encode the string <string> replacing all characters tagged in
486 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
487 * prefixed by <escape>, and will store the result between <start> (included)
488 * and <stop> (excluded), and will always terminate the string with a '\0'
489 * before <stop>. The position of the '\0' is returned if the conversion
490 * completes. If bytes are missing between <start> and <stop>, then the
491 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
492 * cannot even be stored so we return <start> without writing the 0.
493 * The input string must also be zero-terminated.
494 */
495const char hextab[16] = "0123456789ABCDEF";
496char *encode_string(char *start, char *stop,
497 const char escape, const fd_set *map,
498 const char *string)
499{
500 if (start < stop) {
501 stop--; /* reserve one byte for the final '\0' */
502 while (start < stop && *string != '\0') {
503 if (!FD_ISSET((unsigned char)(*string), map))
504 *start++ = *string;
505 else {
506 if (start + 3 >= stop)
507 break;
508 *start++ = escape;
509 *start++ = hextab[(*string >> 4) & 15];
510 *start++ = hextab[*string & 15];
511 }
512 string++;
513 }
514 *start = '\0';
515 }
516 return start;
517}
518
519
Willy Tarreau6911fa42007-03-04 18:06:08 +0100520unsigned int str2ui(const char *s)
521{
522 return __str2ui(s);
523}
524
525unsigned int str2uic(const char *s)
526{
527 return __str2uic(s);
528}
529
530unsigned int strl2ui(const char *s, int len)
531{
532 return __strl2ui(s, len);
533}
534
535unsigned int strl2uic(const char *s, int len)
536{
537 return __strl2uic(s, len);
538}
539
Willy Tarreau4ec83cd2010-10-15 23:19:55 +0200540unsigned int read_uint(const char **s, const char *end)
541{
542 return __read_uint(s, end);
543}
544
Willy Tarreau6911fa42007-03-04 18:06:08 +0100545/* This one is 7 times faster than strtol() on athlon with checks.
546 * It returns the value of the number composed of all valid digits read,
547 * and can process negative numbers too.
548 */
549int strl2ic(const char *s, int len)
550{
551 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200552 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100553
554 if (len > 0) {
555 if (*s != '-') {
556 /* positive number */
557 while (len-- > 0) {
558 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200559 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100560 if (j > 9)
561 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200562 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100563 }
564 } else {
565 /* negative number */
566 s++;
567 while (--len > 0) {
568 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200569 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100570 if (j > 9)
571 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200572 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100573 }
574 }
575 }
576 return i;
577}
578
579
580/* This function reads exactly <len> chars from <s> and converts them to a
581 * signed integer which it stores into <ret>. It accurately detects any error
582 * (truncated string, invalid chars, overflows). It is meant to be used in
583 * applications designed for hostile environments. It returns zero when the
584 * number has successfully been converted, non-zero otherwise. When an error
585 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
586 * faster than strtol().
587 */
588int strl2irc(const char *s, int len, int *ret)
589{
590 int i = 0;
591 int j;
592
593 if (!len)
594 return 1;
595
596 if (*s != '-') {
597 /* positive number */
598 while (len-- > 0) {
599 j = (*s++) - '0';
600 if (j > 9) return 1; /* invalid char */
601 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
602 i = i * 10;
603 if (i + j < i) return 1; /* check for addition overflow */
604 i = i + j;
605 }
606 } else {
607 /* negative number */
608 s++;
609 while (--len > 0) {
610 j = (*s++) - '0';
611 if (j > 9) return 1; /* invalid char */
612 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
613 i = i * 10;
614 if (i - j > i) return 1; /* check for subtract overflow */
615 i = i - j;
616 }
617 }
618 *ret = i;
619 return 0;
620}
621
622
623/* This function reads exactly <len> chars from <s> and converts them to a
624 * signed integer which it stores into <ret>. It accurately detects any error
625 * (truncated string, invalid chars, overflows). It is meant to be used in
626 * applications designed for hostile environments. It returns zero when the
627 * number has successfully been converted, non-zero otherwise. When an error
628 * is returned, the <ret> value is left untouched. It is about 3 times slower
629 * than str2irc().
630 */
631#ifndef LLONG_MAX
632#define LLONG_MAX 9223372036854775807LL
633#define LLONG_MIN (-LLONG_MAX - 1LL)
634#endif
635
636int strl2llrc(const char *s, int len, long long *ret)
637{
638 long long i = 0;
639 int j;
640
641 if (!len)
642 return 1;
643
644 if (*s != '-') {
645 /* positive number */
646 while (len-- > 0) {
647 j = (*s++) - '0';
648 if (j > 9) return 1; /* invalid char */
649 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
650 i = i * 10LL;
651 if (i + j < i) return 1; /* check for addition overflow */
652 i = i + j;
653 }
654 } else {
655 /* negative number */
656 s++;
657 while (--len > 0) {
658 j = (*s++) - '0';
659 if (j > 9) return 1; /* invalid char */
660 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
661 i = i * 10LL;
662 if (i - j > i) return 1; /* check for subtract overflow */
663 i = i - j;
664 }
665 }
666 *ret = i;
667 return 0;
668}
669
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100670/* This function parses a time value optionally followed by a unit suffix among
671 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
672 * expected by the caller. The computation does its best to avoid overflows.
673 * The value is returned in <ret> if everything is fine, and a NULL is returned
674 * by the function. In case of error, a pointer to the error is returned and
675 * <ret> is left untouched. Values are automatically rounded up when needed.
676 */
677const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
678{
679 unsigned imult, idiv;
680 unsigned omult, odiv;
681 unsigned value;
682
683 omult = odiv = 1;
684
685 switch (unit_flags & TIME_UNIT_MASK) {
686 case TIME_UNIT_US: omult = 1000000; break;
687 case TIME_UNIT_MS: omult = 1000; break;
688 case TIME_UNIT_S: break;
689 case TIME_UNIT_MIN: odiv = 60; break;
690 case TIME_UNIT_HOUR: odiv = 3600; break;
691 case TIME_UNIT_DAY: odiv = 86400; break;
692 default: break;
693 }
694
695 value = 0;
696
697 while (1) {
698 unsigned int j;
699
700 j = *text - '0';
701 if (j > 9)
702 break;
703 text++;
704 value *= 10;
705 value += j;
706 }
707
708 imult = idiv = 1;
709 switch (*text) {
710 case '\0': /* no unit = default unit */
711 imult = omult = idiv = odiv = 1;
712 break;
713 case 's': /* second = unscaled unit */
714 break;
715 case 'u': /* microsecond : "us" */
716 if (text[1] == 's') {
717 idiv = 1000000;
718 text++;
719 }
720 break;
721 case 'm': /* millisecond : "ms" or minute: "m" */
722 if (text[1] == 's') {
723 idiv = 1000;
724 text++;
725 } else
726 imult = 60;
727 break;
728 case 'h': /* hour : "h" */
729 imult = 3600;
730 break;
731 case 'd': /* day : "d" */
732 imult = 86400;
733 break;
734 default:
735 return text;
736 break;
737 }
738
739 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
740 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
741 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
742 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
743
744 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
745 *ret = value;
746 return NULL;
747}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100748
Emeric Brun39132b22010-01-04 14:57:24 +0100749/* this function converts the string starting at <text> to an unsigned int
750 * stored in <ret>. If an error is detected, the pointer to the unexpected
751 * character is returned. If the conversio is succesful, NULL is returned.
752 */
753const char *parse_size_err(const char *text, unsigned *ret) {
754 unsigned value = 0;
755
756 while (1) {
757 unsigned int j;
758
759 j = *text - '0';
760 if (j > 9)
761 break;
762 if (value > ~0U / 10)
763 return text;
764 value *= 10;
765 if (value > (value + j))
766 return text;
767 value += j;
768 text++;
769 }
770
771 switch (*text) {
772 case '\0':
773 break;
774 case 'K':
775 case 'k':
776 if (value > ~0U >> 10)
777 return text;
778 value = value << 10;
779 break;
780 case 'M':
781 case 'm':
782 if (value > ~0U >> 20)
783 return text;
784 value = value << 20;
785 break;
786 case 'G':
787 case 'g':
788 if (value > ~0U >> 30)
789 return text;
790 value = value << 30;
791 break;
792 default:
793 return text;
794 }
795
796 *ret = value;
797 return NULL;
798}
799
Willy Tarreau946ba592009-05-10 15:41:18 +0200800/* copies at most <n> characters from <src> and always terminates with '\0' */
801char *my_strndup(const char *src, int n)
802{
803 int len = 0;
804 char *ret;
805
806 while (len < n && src[len])
807 len++;
808
809 ret = (char *)malloc(len + 1);
810 if (!ret)
811 return ret;
812 memcpy(ret, src, len);
813 ret[len] = '\0';
814 return ret;
815}
816
Willy Tarreau482b00d2009-10-04 22:48:42 +0200817/* This function returns the first unused key greater than or equal to <key> in
818 * ID tree <root>. Zero is returned if no place is found.
819 */
820unsigned int get_next_id(struct eb_root *root, unsigned int key)
821{
822 struct eb32_node *used;
823
824 do {
825 used = eb32_lookup_ge(root, key);
826 if (!used || used->key > key)
827 return key; /* key is available */
828 key++;
829 } while (key);
830 return key;
831}
832
Willy Tarreau348238b2010-01-18 15:05:57 +0100833/* This function compares a sample word possibly followed by blanks to another
834 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
835 * otherwise zero. This intends to be used when checking HTTP headers for some
836 * values. Note that it validates a word followed only by blanks but does not
837 * validate a word followed by blanks then other chars.
838 */
839int word_match(const char *sample, int slen, const char *word, int wlen)
840{
841 if (slen < wlen)
842 return 0;
843
844 while (wlen) {
845 char c = *sample ^ *word;
846 if (c && c != ('A' ^ 'a'))
847 return 0;
848 sample++;
849 word++;
850 slen--;
851 wlen--;
852 }
853
854 while (slen) {
855 if (*sample != ' ' && *sample != '\t')
856 return 0;
857 sample++;
858 slen--;
859 }
860 return 1;
861}
Willy Tarreau482b00d2009-10-04 22:48:42 +0200862
Willy Tarreaud54bbdc2009-09-07 11:00:31 +0200863/* Converts any text-formatted IPv4 address to a host-order IPv4 address. It
864 * is particularly fast because it avoids expensive operations such as
865 * multiplies, which are optimized away at the end. It requires a properly
866 * formated address though (3 points).
867 */
868unsigned int inetaddr_host(const char *text)
869{
870 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
871 register unsigned int dig100, dig10, dig1;
872 int s;
873 const char *p, *d;
874
875 dig1 = dig10 = dig100 = ascii_zero;
876 s = 24;
877
878 p = text;
879 while (1) {
880 if (((unsigned)(*p - '0')) <= 9) {
881 p++;
882 continue;
883 }
884
885 /* here, we have a complete byte between <text> and <p> (exclusive) */
886 if (p == text)
887 goto end;
888
889 d = p - 1;
890 dig1 |= (unsigned int)(*d << s);
891 if (d == text)
892 goto end;
893
894 d--;
895 dig10 |= (unsigned int)(*d << s);
896 if (d == text)
897 goto end;
898
899 d--;
900 dig100 |= (unsigned int)(*d << s);
901 end:
902 if (!s || *p != '.')
903 break;
904
905 s -= 8;
906 text = ++p;
907 }
908
909 dig100 -= ascii_zero;
910 dig10 -= ascii_zero;
911 dig1 -= ascii_zero;
912 return ((dig100 * 10) + dig10) * 10 + dig1;
913}
914
915/*
916 * Idem except the first unparsed character has to be passed in <stop>.
917 */
918unsigned int inetaddr_host_lim(const char *text, const char *stop)
919{
920 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
921 register unsigned int dig100, dig10, dig1;
922 int s;
923 const char *p, *d;
924
925 dig1 = dig10 = dig100 = ascii_zero;
926 s = 24;
927
928 p = text;
929 while (1) {
930 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
931 p++;
932 continue;
933 }
934
935 /* here, we have a complete byte between <text> and <p> (exclusive) */
936 if (p == text)
937 goto end;
938
939 d = p - 1;
940 dig1 |= (unsigned int)(*d << s);
941 if (d == text)
942 goto end;
943
944 d--;
945 dig10 |= (unsigned int)(*d << s);
946 if (d == text)
947 goto end;
948
949 d--;
950 dig100 |= (unsigned int)(*d << s);
951 end:
952 if (!s || p == stop || *p != '.')
953 break;
954
955 s -= 8;
956 text = ++p;
957 }
958
959 dig100 -= ascii_zero;
960 dig10 -= ascii_zero;
961 dig1 -= ascii_zero;
962 return ((dig100 * 10) + dig10) * 10 + dig1;
963}
964
965/*
966 * Idem except the pointer to first unparsed byte is returned into <ret> which
967 * must not be NULL.
968 */
Willy Tarreau74172752010-10-15 23:21:42 +0200969unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret)
Willy Tarreaud54bbdc2009-09-07 11:00:31 +0200970{
971 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
972 register unsigned int dig100, dig10, dig1;
973 int s;
Willy Tarreau74172752010-10-15 23:21:42 +0200974 char *p, *d;
Willy Tarreaud54bbdc2009-09-07 11:00:31 +0200975
976 dig1 = dig10 = dig100 = ascii_zero;
977 s = 24;
978
979 p = text;
980 while (1) {
981 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
982 p++;
983 continue;
984 }
985
986 /* here, we have a complete byte between <text> and <p> (exclusive) */
987 if (p == text)
988 goto end;
989
990 d = p - 1;
991 dig1 |= (unsigned int)(*d << s);
992 if (d == text)
993 goto end;
994
995 d--;
996 dig10 |= (unsigned int)(*d << s);
997 if (d == text)
998 goto end;
999
1000 d--;
1001 dig100 |= (unsigned int)(*d << s);
1002 end:
1003 if (!s || p == stop || *p != '.')
1004 break;
1005
1006 s -= 8;
1007 text = ++p;
1008 }
1009
1010 *ret = p;
1011 dig100 -= ascii_zero;
1012 dig10 -= ascii_zero;
1013 dig1 -= ascii_zero;
1014 return ((dig100 * 10) + dig10) * 10 + dig1;
1015}
1016
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001017/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
1018 * or the number of chars read in case of success. Maybe this could be replaced
1019 * by one of the functions above. Also, apparently this function does not support
1020 * hosts above 255 and requires exactly 4 octets.
1021 */
1022int buf2ip(const char *buf, size_t len, struct in_addr *dst)
1023{
1024 const char *addr;
1025 int saw_digit, octets, ch;
1026 u_char tmp[4], *tp;
1027 const char *cp = buf;
1028
1029 saw_digit = 0;
1030 octets = 0;
1031 *(tp = tmp) = 0;
1032
1033 for (addr = buf; addr - buf < len; addr++) {
1034 unsigned char digit = (ch = *addr) - '0';
1035
1036 if (digit > 9 && ch != '.')
1037 break;
1038
1039 if (digit <= 9) {
1040 u_int new = *tp * 10 + digit;
1041
1042 if (new > 255)
1043 return 0;
1044
1045 *tp = new;
1046
1047 if (!saw_digit) {
1048 if (++octets > 4)
1049 return 0;
1050 saw_digit = 1;
1051 }
1052 } else if (ch == '.' && saw_digit) {
1053 if (octets == 4)
1054 return 0;
1055
1056 *++tp = 0;
1057 saw_digit = 0;
1058 } else
1059 return 0;
1060 }
1061
1062 if (octets < 4)
1063 return 0;
1064
1065 memcpy(&dst->s_addr, tmp, 4);
1066 return addr - cp;
1067}
1068
Willy Tarreauacf95772010-06-14 19:09:21 +02001069/* To be used to quote config arg positions. Returns the short string at <ptr>
1070 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
1071 * if ptr is NULL or empty. The string is locally allocated.
1072 */
1073const char *quote_arg(const char *ptr)
1074{
1075 static char val[32];
1076 int i;
1077
1078 if (!ptr || !*ptr)
1079 return "end of line";
1080 val[0] = '\'';
1081 for (i = 1; i < sizeof(val) - 1 && *ptr; i++)
1082 val[i] = *ptr++;
1083 val[i++] = '\'';
1084 val[i] = '\0';
1085 return val;
1086}
1087
Willy Tarreau5b180202010-07-18 10:40:48 +02001088/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
1089int get_std_op(const char *str)
1090{
1091 int ret = -1;
1092
1093 if (*str == 'e' && str[1] == 'q')
1094 ret = STD_OP_EQ;
1095 else if (*str == 'n' && str[1] == 'e')
1096 ret = STD_OP_NE;
1097 else if (*str == 'l') {
1098 if (str[1] == 'e') ret = STD_OP_LE;
1099 else if (str[1] == 't') ret = STD_OP_LT;
1100 }
1101 else if (*str == 'g') {
1102 if (str[1] == 'e') ret = STD_OP_GE;
1103 else if (str[1] == 't') ret = STD_OP_GT;
1104 }
1105
1106 if (ret == -1 || str[2] != '\0')
1107 return -1;
1108 return ret;
1109}
1110
Willy Tarreau4c14eaa2010-11-24 14:01:45 +01001111/* hash a 32-bit integer to another 32-bit integer */
1112unsigned int full_hash(unsigned int a)
1113{
1114 return __full_hash(a);
1115}
1116
Willy Tarreaubaaee002006-06-26 02:48:02 +02001117/*
1118 * Local variables:
1119 * c-indent-level: 8
1120 * c-basic-offset: 8
1121 * End:
1122 */