blob: fff7168ca66777cb0fb5b55c17d5c7788da7991f [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
66
67/*
68 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
69 *
70 * It looks like this one would be a good candidate for inlining, but this is
71 * not interesting because it around 35 bytes long and often called multiple
72 * times within the same function.
73 */
74int ishex(char s)
75{
76 s -= '0';
77 if ((unsigned char)s <= 9)
78 return 1;
79 s -= 'A' - '0';
80 if ((unsigned char)s <= 5)
81 return 1;
82 s -= 'a' - 'A';
83 if ((unsigned char)s <= 5)
84 return 1;
85 return 0;
86}
87
88
89/*
90 * converts <str> to a struct sockaddr_in* which is locally allocated.
91 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
92 * a host name, or empty or "*" to indicate INADDR_ANY.
93 */
94struct sockaddr_in *str2sa(char *str)
95{
96 static struct sockaddr_in sa;
97 char *c;
98 int port;
99
100 memset(&sa, 0, sizeof(sa));
101 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200102 if (str == NULL)
103 goto out_nofree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200104
105 if ((c = strrchr(str,':')) != NULL) {
106 *c++ = '\0';
107 port = atol(c);
108 }
109 else
110 port = 0;
111
112 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
113 sa.sin_addr.s_addr = INADDR_ANY;
114 }
115 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
116 struct hostent *he;
117
118 if ((he = gethostbyname(str)) == NULL) {
119 Alert("Invalid server name: '%s'\n", str);
120 }
121 else
122 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
123 }
124 sa.sin_port = htons(port);
125 sa.sin_family = AF_INET;
126
127 free(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200128 out_nofree:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200129 return &sa;
130}
131
132/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200133 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200134 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
135 * is optionnal and either in the dotted or CIDR notation.
136 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
137 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200138int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200140 __label__ out_free, out_err;
141 char *c, *s;
142 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200143 unsigned long len;
144
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200145 s = strdup(str);
146 if (!s)
147 return 0;
148
Willy Tarreaubaaee002006-06-26 02:48:02 +0200149 memset(mask, 0, sizeof(*mask));
150 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200151
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200152 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200153 *c++ = '\0';
154 /* c points to the mask */
155 if (strchr(c, '.') != NULL) { /* dotted notation */
156 if (!inet_pton(AF_INET, c, mask))
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200157 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200158 }
159 else { /* mask length */
160 char *err;
161 len = strtol(c, &err, 10);
162 if (!*c || (err && *err) || (unsigned)len > 32)
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200163 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200164 if (len)
165 mask->s_addr = htonl(~0UL << (32 - len));
166 else
167 mask->s_addr = 0;
168 }
169 }
170 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100171 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200172 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200173 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200174 struct hostent *he;
175
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200176 if ((he = gethostbyname(s)) == NULL) {
177 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200178 }
179 else
180 *addr = *(struct in_addr *) *(he->h_addr_list);
181 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200182
183 ret_val = 1;
184 out_free:
185 free(s);
186 return ret_val;
187 out_err:
188 ret_val = 0;
189 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200190}
191
192/* will try to encode the string <string> replacing all characters tagged in
193 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
194 * prefixed by <escape>, and will store the result between <start> (included)
195 * and <stop> (excluded), and will always terminate the string with a '\0'
196 * before <stop>. The position of the '\0' is returned if the conversion
197 * completes. If bytes are missing between <start> and <stop>, then the
198 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
199 * cannot even be stored so we return <start> without writing the 0.
200 * The input string must also be zero-terminated.
201 */
202const char hextab[16] = "0123456789ABCDEF";
203char *encode_string(char *start, char *stop,
204 const char escape, const fd_set *map,
205 const char *string)
206{
207 if (start < stop) {
208 stop--; /* reserve one byte for the final '\0' */
209 while (start < stop && *string != '\0') {
210 if (!FD_ISSET((unsigned char)(*string), map))
211 *start++ = *string;
212 else {
213 if (start + 3 >= stop)
214 break;
215 *start++ = escape;
216 *start++ = hextab[(*string >> 4) & 15];
217 *start++ = hextab[*string & 15];
218 }
219 string++;
220 }
221 *start = '\0';
222 }
223 return start;
224}
225
226
Willy Tarreau6911fa42007-03-04 18:06:08 +0100227unsigned int str2ui(const char *s)
228{
229 return __str2ui(s);
230}
231
232unsigned int str2uic(const char *s)
233{
234 return __str2uic(s);
235}
236
237unsigned int strl2ui(const char *s, int len)
238{
239 return __strl2ui(s, len);
240}
241
242unsigned int strl2uic(const char *s, int len)
243{
244 return __strl2uic(s, len);
245}
246
247/* This one is 7 times faster than strtol() on athlon with checks.
248 * It returns the value of the number composed of all valid digits read,
249 * and can process negative numbers too.
250 */
251int strl2ic(const char *s, int len)
252{
253 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200254 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100255
256 if (len > 0) {
257 if (*s != '-') {
258 /* positive number */
259 while (len-- > 0) {
260 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200261 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100262 if (j > 9)
263 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200264 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100265 }
266 } else {
267 /* negative number */
268 s++;
269 while (--len > 0) {
270 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200271 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100272 if (j > 9)
273 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200274 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100275 }
276 }
277 }
278 return i;
279}
280
281
282/* This function reads exactly <len> chars from <s> and converts them to a
283 * signed integer which it stores into <ret>. It accurately detects any error
284 * (truncated string, invalid chars, overflows). It is meant to be used in
285 * applications designed for hostile environments. It returns zero when the
286 * number has successfully been converted, non-zero otherwise. When an error
287 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
288 * faster than strtol().
289 */
290int strl2irc(const char *s, int len, int *ret)
291{
292 int i = 0;
293 int j;
294
295 if (!len)
296 return 1;
297
298 if (*s != '-') {
299 /* positive number */
300 while (len-- > 0) {
301 j = (*s++) - '0';
302 if (j > 9) return 1; /* invalid char */
303 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
304 i = i * 10;
305 if (i + j < i) return 1; /* check for addition overflow */
306 i = i + j;
307 }
308 } else {
309 /* negative number */
310 s++;
311 while (--len > 0) {
312 j = (*s++) - '0';
313 if (j > 9) return 1; /* invalid char */
314 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
315 i = i * 10;
316 if (i - j > i) return 1; /* check for subtract overflow */
317 i = i - j;
318 }
319 }
320 *ret = i;
321 return 0;
322}
323
324
325/* This function reads exactly <len> chars from <s> and converts them to a
326 * signed integer which it stores into <ret>. It accurately detects any error
327 * (truncated string, invalid chars, overflows). It is meant to be used in
328 * applications designed for hostile environments. It returns zero when the
329 * number has successfully been converted, non-zero otherwise. When an error
330 * is returned, the <ret> value is left untouched. It is about 3 times slower
331 * than str2irc().
332 */
333#ifndef LLONG_MAX
334#define LLONG_MAX 9223372036854775807LL
335#define LLONG_MIN (-LLONG_MAX - 1LL)
336#endif
337
338int strl2llrc(const char *s, int len, long long *ret)
339{
340 long long i = 0;
341 int j;
342
343 if (!len)
344 return 1;
345
346 if (*s != '-') {
347 /* positive number */
348 while (len-- > 0) {
349 j = (*s++) - '0';
350 if (j > 9) return 1; /* invalid char */
351 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
352 i = i * 10LL;
353 if (i + j < i) return 1; /* check for addition overflow */
354 i = i + j;
355 }
356 } else {
357 /* negative number */
358 s++;
359 while (--len > 0) {
360 j = (*s++) - '0';
361 if (j > 9) return 1; /* invalid char */
362 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
363 i = i * 10LL;
364 if (i - j > i) return 1; /* check for subtract overflow */
365 i = i - j;
366 }
367 }
368 *ret = i;
369 return 0;
370}
371
372
Willy Tarreaubaaee002006-06-26 02:48:02 +0200373/*
374 * Local variables:
375 * c-indent-level: 8
376 * c-basic-offset: 8
377 * End:
378 */