blob: d245949a99e5d721e684cc1387e6013e7232a5bd [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
13#include <netdb.h>
14#include <stdlib.h>
15#include <string.h>
16#include <netinet/in.h>
17#include <arpa/inet.h>
18
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020019#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020020#include <common/standard.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020021#include <proto/log.h>
22
Willy Tarreau72d759c2007-10-25 12:14:10 +020023/* enough to store 10 integers of :
24 * 2^64-1 = 18446744073709551615 or
25 * -2^63 = -9223372036854775808
26 */
27char itoa_str[10][21];
Willy Tarreaubaaee002006-06-26 02:48:02 +020028
29/*
30 * copies at most <size-1> chars from <src> to <dst>. Last char is always
31 * set to 0, unless <size> is 0. The number of chars copied is returned
32 * (excluding the terminating zero).
33 * This code has been optimized for size and speed : on x86, it's 45 bytes
34 * long, uses only registers, and consumes only 4 cycles per char.
35 */
36int strlcpy2(char *dst, const char *src, int size)
37{
38 char *orig = dst;
39 if (size) {
40 while (--size && (*dst = *src)) {
41 src++; dst++;
42 }
43 *dst = 0;
44 }
45 return dst - orig;
46}
47
48/*
Willy Tarreau72d759c2007-10-25 12:14:10 +020049 * This function simply returns a locally allocated string containing
Willy Tarreaubaaee002006-06-26 02:48:02 +020050 * the ascii representation for number 'n' in decimal.
51 */
Willy Tarreau72d759c2007-10-25 12:14:10 +020052const char *ultoa_r(unsigned long n, char *buffer, int size)
Willy Tarreaubaaee002006-06-26 02:48:02 +020053{
54 char *pos;
55
Willy Tarreau72d759c2007-10-25 12:14:10 +020056 pos = buffer + size - 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020057 *pos-- = '\0';
58
59 do {
60 *pos-- = '0' + n % 10;
61 n /= 10;
Willy Tarreau72d759c2007-10-25 12:14:10 +020062 } while (n && pos >= buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +020063 return pos + 1;
64}
65
Willy Tarreau91092e52007-10-25 16:58:42 +020066/*
67 * This function simply returns a locally allocated string containing the ascii
68 * representation for number 'n' in decimal, unless n is 0 in which case it
69 * returns the alternate string (or an empty string if the alternate string is
70 * NULL). It use is intended for limits reported in reports, where it's
71 * desirable not to display anything if there is no limit. Warning! it shares
72 * the same vector as ultoa_r().
73 */
74const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
75{
76 return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
77}
78
Willy Tarreaubaaee002006-06-26 02:48:02 +020079
80/*
81 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
82 *
83 * It looks like this one would be a good candidate for inlining, but this is
84 * not interesting because it around 35 bytes long and often called multiple
85 * times within the same function.
86 */
87int ishex(char s)
88{
89 s -= '0';
90 if ((unsigned char)s <= 9)
91 return 1;
92 s -= 'A' - '0';
93 if ((unsigned char)s <= 5)
94 return 1;
95 s -= 'a' - 'A';
96 if ((unsigned char)s <= 5)
97 return 1;
98 return 0;
99}
100
101
102/*
103 * converts <str> to a struct sockaddr_in* which is locally allocated.
104 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
105 * a host name, or empty or "*" to indicate INADDR_ANY.
106 */
107struct sockaddr_in *str2sa(char *str)
108{
109 static struct sockaddr_in sa;
110 char *c;
111 int port;
112
113 memset(&sa, 0, sizeof(sa));
114 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200115 if (str == NULL)
116 goto out_nofree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200117
118 if ((c = strrchr(str,':')) != NULL) {
119 *c++ = '\0';
120 port = atol(c);
121 }
122 else
123 port = 0;
124
125 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
126 sa.sin_addr.s_addr = INADDR_ANY;
127 }
128 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
129 struct hostent *he;
130
131 if ((he = gethostbyname(str)) == NULL) {
132 Alert("Invalid server name: '%s'\n", str);
133 }
134 else
135 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
136 }
137 sa.sin_port = htons(port);
138 sa.sin_family = AF_INET;
139
140 free(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200141 out_nofree:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200142 return &sa;
143}
144
145/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200146 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200147 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
148 * is optionnal and either in the dotted or CIDR notation.
149 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
150 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200151int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200152{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200153 __label__ out_free, out_err;
154 char *c, *s;
155 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200156 unsigned long len;
157
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200158 s = strdup(str);
159 if (!s)
160 return 0;
161
Willy Tarreaubaaee002006-06-26 02:48:02 +0200162 memset(mask, 0, sizeof(*mask));
163 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200165 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 *c++ = '\0';
167 /* c points to the mask */
168 if (strchr(c, '.') != NULL) { /* dotted notation */
169 if (!inet_pton(AF_INET, c, mask))
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200170 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200171 }
172 else { /* mask length */
173 char *err;
174 len = strtol(c, &err, 10);
175 if (!*c || (err && *err) || (unsigned)len > 32)
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200176 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200177 if (len)
178 mask->s_addr = htonl(~0UL << (32 - len));
179 else
180 mask->s_addr = 0;
181 }
182 }
183 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100184 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200185 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200186 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200187 struct hostent *he;
188
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200189 if ((he = gethostbyname(s)) == NULL) {
190 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200191 }
192 else
193 *addr = *(struct in_addr *) *(he->h_addr_list);
194 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200195
196 ret_val = 1;
197 out_free:
198 free(s);
199 return ret_val;
200 out_err:
201 ret_val = 0;
202 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200203}
204
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100205
206/*
207 * Parse IP address found in url.
208 */
209static int url2ip(const char *addr, struct in_addr *dst)
210{
211 int saw_digit, octets, ch;
212 u_char tmp[4], *tp;
213 const char *cp = addr;
214
215 saw_digit = 0;
216 octets = 0;
217 *(tp = tmp) = 0;
218
219 while (*addr) {
220 unsigned char digit = (ch = *addr++) - '0';
221 if (digit > 9 && ch != '.')
222 break;
223 if (digit <= 9) {
224 u_int new = *tp * 10 + digit;
225 if (new > 255)
226 return 0;
227 *tp = new;
228 if (!saw_digit) {
229 if (++octets > 4)
230 return 0;
231 saw_digit = 1;
232 }
233 } else if (ch == '.' && saw_digit) {
234 if (octets == 4)
235 return 0;
236 *++tp = 0;
237 saw_digit = 0;
238 } else
239 return 0;
240 }
241
242 if (octets < 4)
243 return 0;
244
245 memcpy(&dst->s_addr, tmp, 4);
246 return addr-cp-1;
247}
248
249/*
250 * Resolve destination server from URL. Convert <str> to a sockaddr_in*.
251 */
252int url2sa(const char *url, int ulen, struct sockaddr_in *addr)
253{
254 const char *curr = url, *cp = url;
255 int ret, url_code = 0;
256 unsigned int http_code = 0;
257
258 /* Cleanup the room */
259 addr->sin_family = AF_INET;
260 addr->sin_addr.s_addr = 0;
261 addr->sin_port = 0;
262
263 /* Firstly, try to find :// pattern */
264 while (curr < url+ulen && url_code != 0x3a2f2f) {
265 url_code = ((url_code & 0xffff) << 8);
266 url_code += (unsigned char)*curr++;
267 }
268
269 /* Secondly, if :// pattern is found, verify parsed stuff
270 * before pattern is matching our http pattern.
271 * If so parse ip address and port in uri.
272 *
273 * WARNING: Current code doesn't support dynamic async dns resolver.
274 */
275 if (url_code == 0x3a2f2f) {
276 while (cp < curr - 3)
277 http_code = (http_code << 8) + *cp++;
278 http_code |= 0x20202020; /* Turn everything to lower case */
279
280 /* HTTP url matching */
281 if (http_code == 0x68747470) {
282 /* We are looking for IP address. If you want to parse and
283 * resolve hostname found in url, you can use str2sa(), but
284 * be warned this can slow down global daemon performances
285 * while handling lagging dns responses.
286 */
287 ret = url2ip(curr, &addr->sin_addr);
288 if (!ret)
289 return -1;
290 curr += ret;
291 addr->sin_port = (*curr == ':') ? htons(str2uic(++curr)) : htons(80);
292 }
293 return 0;
294 }
295
296 return -1;
297}
298
Willy Tarreaubaaee002006-06-26 02:48:02 +0200299/* will try to encode the string <string> replacing all characters tagged in
300 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
301 * prefixed by <escape>, and will store the result between <start> (included)
302 * and <stop> (excluded), and will always terminate the string with a '\0'
303 * before <stop>. The position of the '\0' is returned if the conversion
304 * completes. If bytes are missing between <start> and <stop>, then the
305 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
306 * cannot even be stored so we return <start> without writing the 0.
307 * The input string must also be zero-terminated.
308 */
309const char hextab[16] = "0123456789ABCDEF";
310char *encode_string(char *start, char *stop,
311 const char escape, const fd_set *map,
312 const char *string)
313{
314 if (start < stop) {
315 stop--; /* reserve one byte for the final '\0' */
316 while (start < stop && *string != '\0') {
317 if (!FD_ISSET((unsigned char)(*string), map))
318 *start++ = *string;
319 else {
320 if (start + 3 >= stop)
321 break;
322 *start++ = escape;
323 *start++ = hextab[(*string >> 4) & 15];
324 *start++ = hextab[*string & 15];
325 }
326 string++;
327 }
328 *start = '\0';
329 }
330 return start;
331}
332
333
Willy Tarreau6911fa42007-03-04 18:06:08 +0100334unsigned int str2ui(const char *s)
335{
336 return __str2ui(s);
337}
338
339unsigned int str2uic(const char *s)
340{
341 return __str2uic(s);
342}
343
344unsigned int strl2ui(const char *s, int len)
345{
346 return __strl2ui(s, len);
347}
348
349unsigned int strl2uic(const char *s, int len)
350{
351 return __strl2uic(s, len);
352}
353
354/* This one is 7 times faster than strtol() on athlon with checks.
355 * It returns the value of the number composed of all valid digits read,
356 * and can process negative numbers too.
357 */
358int strl2ic(const char *s, int len)
359{
360 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200361 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100362
363 if (len > 0) {
364 if (*s != '-') {
365 /* positive number */
366 while (len-- > 0) {
367 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200368 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100369 if (j > 9)
370 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200371 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100372 }
373 } else {
374 /* negative number */
375 s++;
376 while (--len > 0) {
377 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200378 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100379 if (j > 9)
380 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200381 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100382 }
383 }
384 }
385 return i;
386}
387
388
389/* This function reads exactly <len> chars from <s> and converts them to a
390 * signed integer which it stores into <ret>. It accurately detects any error
391 * (truncated string, invalid chars, overflows). It is meant to be used in
392 * applications designed for hostile environments. It returns zero when the
393 * number has successfully been converted, non-zero otherwise. When an error
394 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
395 * faster than strtol().
396 */
397int strl2irc(const char *s, int len, int *ret)
398{
399 int i = 0;
400 int j;
401
402 if (!len)
403 return 1;
404
405 if (*s != '-') {
406 /* positive number */
407 while (len-- > 0) {
408 j = (*s++) - '0';
409 if (j > 9) return 1; /* invalid char */
410 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
411 i = i * 10;
412 if (i + j < i) return 1; /* check for addition overflow */
413 i = i + j;
414 }
415 } else {
416 /* negative number */
417 s++;
418 while (--len > 0) {
419 j = (*s++) - '0';
420 if (j > 9) return 1; /* invalid char */
421 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
422 i = i * 10;
423 if (i - j > i) return 1; /* check for subtract overflow */
424 i = i - j;
425 }
426 }
427 *ret = i;
428 return 0;
429}
430
431
432/* This function reads exactly <len> chars from <s> and converts them to a
433 * signed integer which it stores into <ret>. It accurately detects any error
434 * (truncated string, invalid chars, overflows). It is meant to be used in
435 * applications designed for hostile environments. It returns zero when the
436 * number has successfully been converted, non-zero otherwise. When an error
437 * is returned, the <ret> value is left untouched. It is about 3 times slower
438 * than str2irc().
439 */
440#ifndef LLONG_MAX
441#define LLONG_MAX 9223372036854775807LL
442#define LLONG_MIN (-LLONG_MAX - 1LL)
443#endif
444
445int strl2llrc(const char *s, int len, long long *ret)
446{
447 long long i = 0;
448 int j;
449
450 if (!len)
451 return 1;
452
453 if (*s != '-') {
454 /* positive number */
455 while (len-- > 0) {
456 j = (*s++) - '0';
457 if (j > 9) return 1; /* invalid char */
458 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
459 i = i * 10LL;
460 if (i + j < i) return 1; /* check for addition overflow */
461 i = i + j;
462 }
463 } else {
464 /* negative number */
465 s++;
466 while (--len > 0) {
467 j = (*s++) - '0';
468 if (j > 9) return 1; /* invalid char */
469 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
470 i = i * 10LL;
471 if (i - j > i) return 1; /* check for subtract overflow */
472 i = i - j;
473 }
474 }
475 *ret = i;
476 return 0;
477}
478
479
Willy Tarreaubaaee002006-06-26 02:48:02 +0200480/*
481 * Local variables:
482 * c-indent-level: 8
483 * c-basic-offset: 8
484 * End:
485 */