blob: 40ad47e0c23370de0f3871f4023f77d02698a2b5 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General purpose functions.
3 *
Willy Tarreau6911fa42007-03-04 18:06:08 +01004 * Copyright 2000-2007 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>
17#include <netinet/in.h>
18#include <arpa/inet.h>
19
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020020#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020021#include <common/standard.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020022#include <proto/log.h>
23
Willy Tarreau72d759c2007-10-25 12:14:10 +020024/* enough to store 10 integers of :
25 * 2^64-1 = 18446744073709551615 or
26 * -2^63 = -9223372036854775808
27 */
28char itoa_str[10][21];
Willy Tarreaubaaee002006-06-26 02:48:02 +020029
30/*
31 * copies at most <size-1> chars from <src> to <dst>. Last char is always
32 * set to 0, unless <size> is 0. The number of chars copied is returned
33 * (excluding the terminating zero).
34 * This code has been optimized for size and speed : on x86, it's 45 bytes
35 * long, uses only registers, and consumes only 4 cycles per char.
36 */
37int strlcpy2(char *dst, const char *src, int size)
38{
39 char *orig = dst;
40 if (size) {
41 while (--size && (*dst = *src)) {
42 src++; dst++;
43 }
44 *dst = 0;
45 }
46 return dst - orig;
47}
48
49/*
Willy Tarreau72d759c2007-10-25 12:14:10 +020050 * This function simply returns a locally allocated string containing
Willy Tarreaubaaee002006-06-26 02:48:02 +020051 * the ascii representation for number 'n' in decimal.
52 */
Willy Tarreau72d759c2007-10-25 12:14:10 +020053const char *ultoa_r(unsigned long n, char *buffer, int size)
Willy Tarreaubaaee002006-06-26 02:48:02 +020054{
55 char *pos;
56
Willy Tarreau72d759c2007-10-25 12:14:10 +020057 pos = buffer + size - 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 *pos-- = '\0';
59
60 do {
61 *pos-- = '0' + n % 10;
62 n /= 10;
Willy Tarreau72d759c2007-10-25 12:14:10 +020063 } while (n && pos >= buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +020064 return pos + 1;
65}
66
Willy Tarreau91092e52007-10-25 16:58:42 +020067/*
68 * This function simply returns a locally allocated string containing the ascii
69 * representation for number 'n' in decimal, unless n is 0 in which case it
70 * returns the alternate string (or an empty string if the alternate string is
71 * NULL). It use is intended for limits reported in reports, where it's
72 * desirable not to display anything if there is no limit. Warning! it shares
73 * the same vector as ultoa_r().
74 */
75const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
76{
77 return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
78}
79
Willy Tarreaubaaee002006-06-26 02:48:02 +020080
81/*
82 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
83 *
84 * It looks like this one would be a good candidate for inlining, but this is
85 * not interesting because it around 35 bytes long and often called multiple
86 * times within the same function.
87 */
88int ishex(char s)
89{
90 s -= '0';
91 if ((unsigned char)s <= 9)
92 return 1;
93 s -= 'A' - '0';
94 if ((unsigned char)s <= 5)
95 return 1;
96 s -= 'a' - 'A';
97 if ((unsigned char)s <= 5)
98 return 1;
99 return 0;
100}
101
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100102/*
103 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
104 * invalid character is found, a pointer to it is returned. If everything is
105 * fine, NULL is returned.
106 */
107const char *invalid_char(const char *name)
108{
109 if (!*name)
110 return name;
111
112 while (*name) {
113 if (!isalnum(*name) && *name != '.' && *name != ':' &&
114 *name != '_' && *name != '-')
115 return name;
116 name++;
117 }
118 return NULL;
119}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200120
121/*
122 * converts <str> to a struct sockaddr_in* which is locally allocated.
123 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
124 * a host name, or empty or "*" to indicate INADDR_ANY.
125 */
126struct sockaddr_in *str2sa(char *str)
127{
128 static struct sockaddr_in sa;
129 char *c;
130 int port;
131
132 memset(&sa, 0, sizeof(sa));
133 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200134 if (str == NULL)
135 goto out_nofree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200136
137 if ((c = strrchr(str,':')) != NULL) {
138 *c++ = '\0';
139 port = atol(c);
140 }
141 else
142 port = 0;
143
144 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
145 sa.sin_addr.s_addr = INADDR_ANY;
146 }
147 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
148 struct hostent *he;
149
150 if ((he = gethostbyname(str)) == NULL) {
151 Alert("Invalid server name: '%s'\n", str);
152 }
153 else
154 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
155 }
156 sa.sin_port = htons(port);
157 sa.sin_family = AF_INET;
158
159 free(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200160 out_nofree:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200161 return &sa;
162}
163
164/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200165 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
167 * is optionnal and either in the dotted or CIDR notation.
168 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
169 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200170int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200172 __label__ out_free, out_err;
173 char *c, *s;
174 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200175 unsigned long len;
176
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200177 s = strdup(str);
178 if (!s)
179 return 0;
180
Willy Tarreaubaaee002006-06-26 02:48:02 +0200181 memset(mask, 0, sizeof(*mask));
182 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200183
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200184 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185 *c++ = '\0';
186 /* c points to the mask */
187 if (strchr(c, '.') != NULL) { /* dotted notation */
188 if (!inet_pton(AF_INET, c, mask))
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200189 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190 }
191 else { /* mask length */
192 char *err;
193 len = strtol(c, &err, 10);
194 if (!*c || (err && *err) || (unsigned)len > 32)
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200195 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200196 if (len)
197 mask->s_addr = htonl(~0UL << (32 - len));
198 else
199 mask->s_addr = 0;
200 }
201 }
202 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100203 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200204 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200205 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200206 struct hostent *he;
207
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200208 if ((he = gethostbyname(s)) == NULL) {
209 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200210 }
211 else
212 *addr = *(struct in_addr *) *(he->h_addr_list);
213 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200214
215 ret_val = 1;
216 out_free:
217 free(s);
218 return ret_val;
219 out_err:
220 ret_val = 0;
221 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200222}
223
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100224
225/*
226 * Parse IP address found in url.
227 */
228static int url2ip(const char *addr, struct in_addr *dst)
229{
230 int saw_digit, octets, ch;
231 u_char tmp[4], *tp;
232 const char *cp = addr;
233
234 saw_digit = 0;
235 octets = 0;
236 *(tp = tmp) = 0;
237
238 while (*addr) {
239 unsigned char digit = (ch = *addr++) - '0';
240 if (digit > 9 && ch != '.')
241 break;
242 if (digit <= 9) {
243 u_int new = *tp * 10 + digit;
244 if (new > 255)
245 return 0;
246 *tp = new;
247 if (!saw_digit) {
248 if (++octets > 4)
249 return 0;
250 saw_digit = 1;
251 }
252 } else if (ch == '.' && saw_digit) {
253 if (octets == 4)
254 return 0;
255 *++tp = 0;
256 saw_digit = 0;
257 } else
258 return 0;
259 }
260
261 if (octets < 4)
262 return 0;
263
264 memcpy(&dst->s_addr, tmp, 4);
265 return addr-cp-1;
266}
267
268/*
269 * Resolve destination server from URL. Convert <str> to a sockaddr_in*.
270 */
271int url2sa(const char *url, int ulen, struct sockaddr_in *addr)
272{
273 const char *curr = url, *cp = url;
274 int ret, url_code = 0;
275 unsigned int http_code = 0;
276
277 /* Cleanup the room */
278 addr->sin_family = AF_INET;
279 addr->sin_addr.s_addr = 0;
280 addr->sin_port = 0;
281
282 /* Firstly, try to find :// pattern */
283 while (curr < url+ulen && url_code != 0x3a2f2f) {
284 url_code = ((url_code & 0xffff) << 8);
285 url_code += (unsigned char)*curr++;
286 }
287
288 /* Secondly, if :// pattern is found, verify parsed stuff
289 * before pattern is matching our http pattern.
290 * If so parse ip address and port in uri.
291 *
292 * WARNING: Current code doesn't support dynamic async dns resolver.
293 */
294 if (url_code == 0x3a2f2f) {
295 while (cp < curr - 3)
296 http_code = (http_code << 8) + *cp++;
297 http_code |= 0x20202020; /* Turn everything to lower case */
298
299 /* HTTP url matching */
300 if (http_code == 0x68747470) {
301 /* We are looking for IP address. If you want to parse and
302 * resolve hostname found in url, you can use str2sa(), but
303 * be warned this can slow down global daemon performances
304 * while handling lagging dns responses.
305 */
306 ret = url2ip(curr, &addr->sin_addr);
307 if (!ret)
308 return -1;
309 curr += ret;
Willy Tarreaud1cd2762007-12-02 10:55:56 +0100310 addr->sin_port = (*curr == ':') ? str2uic(++curr) : 80;
311 addr->sin_port = htons(addr->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100312 }
313 return 0;
314 }
315
316 return -1;
317}
318
Willy Tarreaubaaee002006-06-26 02:48:02 +0200319/* will try to encode the string <string> replacing all characters tagged in
320 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
321 * prefixed by <escape>, and will store the result between <start> (included)
322 * and <stop> (excluded), and will always terminate the string with a '\0'
323 * before <stop>. The position of the '\0' is returned if the conversion
324 * completes. If bytes are missing between <start> and <stop>, then the
325 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
326 * cannot even be stored so we return <start> without writing the 0.
327 * The input string must also be zero-terminated.
328 */
329const char hextab[16] = "0123456789ABCDEF";
330char *encode_string(char *start, char *stop,
331 const char escape, const fd_set *map,
332 const char *string)
333{
334 if (start < stop) {
335 stop--; /* reserve one byte for the final '\0' */
336 while (start < stop && *string != '\0') {
337 if (!FD_ISSET((unsigned char)(*string), map))
338 *start++ = *string;
339 else {
340 if (start + 3 >= stop)
341 break;
342 *start++ = escape;
343 *start++ = hextab[(*string >> 4) & 15];
344 *start++ = hextab[*string & 15];
345 }
346 string++;
347 }
348 *start = '\0';
349 }
350 return start;
351}
352
353
Willy Tarreau6911fa42007-03-04 18:06:08 +0100354unsigned int str2ui(const char *s)
355{
356 return __str2ui(s);
357}
358
359unsigned int str2uic(const char *s)
360{
361 return __str2uic(s);
362}
363
364unsigned int strl2ui(const char *s, int len)
365{
366 return __strl2ui(s, len);
367}
368
369unsigned int strl2uic(const char *s, int len)
370{
371 return __strl2uic(s, len);
372}
373
374/* This one is 7 times faster than strtol() on athlon with checks.
375 * It returns the value of the number composed of all valid digits read,
376 * and can process negative numbers too.
377 */
378int strl2ic(const char *s, int len)
379{
380 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200381 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100382
383 if (len > 0) {
384 if (*s != '-') {
385 /* positive number */
386 while (len-- > 0) {
387 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200388 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100389 if (j > 9)
390 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200391 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100392 }
393 } else {
394 /* negative number */
395 s++;
396 while (--len > 0) {
397 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200398 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100399 if (j > 9)
400 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200401 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100402 }
403 }
404 }
405 return i;
406}
407
408
409/* This function reads exactly <len> chars from <s> and converts them to a
410 * signed integer which it stores into <ret>. It accurately detects any error
411 * (truncated string, invalid chars, overflows). It is meant to be used in
412 * applications designed for hostile environments. It returns zero when the
413 * number has successfully been converted, non-zero otherwise. When an error
414 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
415 * faster than strtol().
416 */
417int strl2irc(const char *s, int len, int *ret)
418{
419 int i = 0;
420 int j;
421
422 if (!len)
423 return 1;
424
425 if (*s != '-') {
426 /* positive number */
427 while (len-- > 0) {
428 j = (*s++) - '0';
429 if (j > 9) return 1; /* invalid char */
430 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
431 i = i * 10;
432 if (i + j < i) return 1; /* check for addition overflow */
433 i = i + j;
434 }
435 } else {
436 /* negative number */
437 s++;
438 while (--len > 0) {
439 j = (*s++) - '0';
440 if (j > 9) return 1; /* invalid char */
441 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
442 i = i * 10;
443 if (i - j > i) return 1; /* check for subtract overflow */
444 i = i - j;
445 }
446 }
447 *ret = i;
448 return 0;
449}
450
451
452/* This function reads exactly <len> chars from <s> and converts them to a
453 * signed integer which it stores into <ret>. It accurately detects any error
454 * (truncated string, invalid chars, overflows). It is meant to be used in
455 * applications designed for hostile environments. It returns zero when the
456 * number has successfully been converted, non-zero otherwise. When an error
457 * is returned, the <ret> value is left untouched. It is about 3 times slower
458 * than str2irc().
459 */
460#ifndef LLONG_MAX
461#define LLONG_MAX 9223372036854775807LL
462#define LLONG_MIN (-LLONG_MAX - 1LL)
463#endif
464
465int strl2llrc(const char *s, int len, long long *ret)
466{
467 long long i = 0;
468 int j;
469
470 if (!len)
471 return 1;
472
473 if (*s != '-') {
474 /* positive number */
475 while (len-- > 0) {
476 j = (*s++) - '0';
477 if (j > 9) return 1; /* invalid char */
478 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
479 i = i * 10LL;
480 if (i + j < i) return 1; /* check for addition overflow */
481 i = i + j;
482 }
483 } else {
484 /* negative number */
485 s++;
486 while (--len > 0) {
487 j = (*s++) - '0';
488 if (j > 9) return 1; /* invalid char */
489 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
490 i = i * 10LL;
491 if (i - j > i) return 1; /* check for subtract overflow */
492 i = i - j;
493 }
494 }
495 *ret = i;
496 return 0;
497}
498
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100499/* This function parses a time value optionally followed by a unit suffix among
500 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
501 * expected by the caller. The computation does its best to avoid overflows.
502 * The value is returned in <ret> if everything is fine, and a NULL is returned
503 * by the function. In case of error, a pointer to the error is returned and
504 * <ret> is left untouched. Values are automatically rounded up when needed.
505 */
506const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
507{
508 unsigned imult, idiv;
509 unsigned omult, odiv;
510 unsigned value;
511
512 omult = odiv = 1;
513
514 switch (unit_flags & TIME_UNIT_MASK) {
515 case TIME_UNIT_US: omult = 1000000; break;
516 case TIME_UNIT_MS: omult = 1000; break;
517 case TIME_UNIT_S: break;
518 case TIME_UNIT_MIN: odiv = 60; break;
519 case TIME_UNIT_HOUR: odiv = 3600; break;
520 case TIME_UNIT_DAY: odiv = 86400; break;
521 default: break;
522 }
523
524 value = 0;
525
526 while (1) {
527 unsigned int j;
528
529 j = *text - '0';
530 if (j > 9)
531 break;
532 text++;
533 value *= 10;
534 value += j;
535 }
536
537 imult = idiv = 1;
538 switch (*text) {
539 case '\0': /* no unit = default unit */
540 imult = omult = idiv = odiv = 1;
541 break;
542 case 's': /* second = unscaled unit */
543 break;
544 case 'u': /* microsecond : "us" */
545 if (text[1] == 's') {
546 idiv = 1000000;
547 text++;
548 }
549 break;
550 case 'm': /* millisecond : "ms" or minute: "m" */
551 if (text[1] == 's') {
552 idiv = 1000;
553 text++;
554 } else
555 imult = 60;
556 break;
557 case 'h': /* hour : "h" */
558 imult = 3600;
559 break;
560 case 'd': /* day : "d" */
561 imult = 86400;
562 break;
563 default:
564 return text;
565 break;
566 }
567
568 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
569 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
570 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
571 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
572
573 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
574 *ret = value;
575 return NULL;
576}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100577
Willy Tarreaubaaee002006-06-26 02:48:02 +0200578/*
579 * Local variables:
580 * c-indent-level: 8
581 * c-basic-offset: 8
582 * End:
583 */