blob: 1e631301c587a45aa77f378ad211cd042424311c [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
205/* will try to encode the string <string> replacing all characters tagged in
206 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
207 * prefixed by <escape>, and will store the result between <start> (included)
208 * and <stop> (excluded), and will always terminate the string with a '\0'
209 * before <stop>. The position of the '\0' is returned if the conversion
210 * completes. If bytes are missing between <start> and <stop>, then the
211 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
212 * cannot even be stored so we return <start> without writing the 0.
213 * The input string must also be zero-terminated.
214 */
215const char hextab[16] = "0123456789ABCDEF";
216char *encode_string(char *start, char *stop,
217 const char escape, const fd_set *map,
218 const char *string)
219{
220 if (start < stop) {
221 stop--; /* reserve one byte for the final '\0' */
222 while (start < stop && *string != '\0') {
223 if (!FD_ISSET((unsigned char)(*string), map))
224 *start++ = *string;
225 else {
226 if (start + 3 >= stop)
227 break;
228 *start++ = escape;
229 *start++ = hextab[(*string >> 4) & 15];
230 *start++ = hextab[*string & 15];
231 }
232 string++;
233 }
234 *start = '\0';
235 }
236 return start;
237}
238
239
Willy Tarreau6911fa42007-03-04 18:06:08 +0100240unsigned int str2ui(const char *s)
241{
242 return __str2ui(s);
243}
244
245unsigned int str2uic(const char *s)
246{
247 return __str2uic(s);
248}
249
250unsigned int strl2ui(const char *s, int len)
251{
252 return __strl2ui(s, len);
253}
254
255unsigned int strl2uic(const char *s, int len)
256{
257 return __strl2uic(s, len);
258}
259
260/* This one is 7 times faster than strtol() on athlon with checks.
261 * It returns the value of the number composed of all valid digits read,
262 * and can process negative numbers too.
263 */
264int strl2ic(const char *s, int len)
265{
266 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200267 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100268
269 if (len > 0) {
270 if (*s != '-') {
271 /* positive number */
272 while (len-- > 0) {
273 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200274 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100275 if (j > 9)
276 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200277 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100278 }
279 } else {
280 /* negative number */
281 s++;
282 while (--len > 0) {
283 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200284 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100285 if (j > 9)
286 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200287 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100288 }
289 }
290 }
291 return i;
292}
293
294
295/* This function reads exactly <len> chars from <s> and converts them to a
296 * signed integer which it stores into <ret>. It accurately detects any error
297 * (truncated string, invalid chars, overflows). It is meant to be used in
298 * applications designed for hostile environments. It returns zero when the
299 * number has successfully been converted, non-zero otherwise. When an error
300 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
301 * faster than strtol().
302 */
303int strl2irc(const char *s, int len, int *ret)
304{
305 int i = 0;
306 int j;
307
308 if (!len)
309 return 1;
310
311 if (*s != '-') {
312 /* positive number */
313 while (len-- > 0) {
314 j = (*s++) - '0';
315 if (j > 9) return 1; /* invalid char */
316 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
317 i = i * 10;
318 if (i + j < i) return 1; /* check for addition overflow */
319 i = i + j;
320 }
321 } else {
322 /* negative number */
323 s++;
324 while (--len > 0) {
325 j = (*s++) - '0';
326 if (j > 9) return 1; /* invalid char */
327 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
328 i = i * 10;
329 if (i - j > i) return 1; /* check for subtract overflow */
330 i = i - j;
331 }
332 }
333 *ret = i;
334 return 0;
335}
336
337
338/* This function reads exactly <len> chars from <s> and converts them to a
339 * signed integer which it stores into <ret>. It accurately detects any error
340 * (truncated string, invalid chars, overflows). It is meant to be used in
341 * applications designed for hostile environments. It returns zero when the
342 * number has successfully been converted, non-zero otherwise. When an error
343 * is returned, the <ret> value is left untouched. It is about 3 times slower
344 * than str2irc().
345 */
346#ifndef LLONG_MAX
347#define LLONG_MAX 9223372036854775807LL
348#define LLONG_MIN (-LLONG_MAX - 1LL)
349#endif
350
351int strl2llrc(const char *s, int len, long long *ret)
352{
353 long long i = 0;
354 int j;
355
356 if (!len)
357 return 1;
358
359 if (*s != '-') {
360 /* positive number */
361 while (len-- > 0) {
362 j = (*s++) - '0';
363 if (j > 9) return 1; /* invalid char */
364 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
365 i = i * 10LL;
366 if (i + j < i) return 1; /* check for addition overflow */
367 i = i + j;
368 }
369 } else {
370 /* negative number */
371 s++;
372 while (--len > 0) {
373 j = (*s++) - '0';
374 if (j > 9) return 1; /* invalid char */
375 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
376 i = i * 10LL;
377 if (i - j > i) return 1; /* check for subtract overflow */
378 i = i - j;
379 }
380 }
381 *ret = i;
382 return 0;
383}
384
385
Willy Tarreaubaaee002006-06-26 02:48:02 +0200386/*
387 * Local variables:
388 * c-indent-level: 8
389 * c-basic-offset: 8
390 * End:
391 */