blob: 5405aaf80bee8479ffbf4dd055aad094b6f514e0 [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 Tarreau6911fa42007-03-04 18:06:08 +010023/* enough to store 2^64-1 = 18446744073709551615 */
Willy Tarreaubaaee002006-06-26 02:48:02 +020024static char itoa_str[21];
25
26/*
27 * copies at most <size-1> chars from <src> to <dst>. Last char is always
28 * set to 0, unless <size> is 0. The number of chars copied is returned
29 * (excluding the terminating zero).
30 * This code has been optimized for size and speed : on x86, it's 45 bytes
31 * long, uses only registers, and consumes only 4 cycles per char.
32 */
33int strlcpy2(char *dst, const char *src, int size)
34{
35 char *orig = dst;
36 if (size) {
37 while (--size && (*dst = *src)) {
38 src++; dst++;
39 }
40 *dst = 0;
41 }
42 return dst - orig;
43}
44
45/*
46 * This function simply returns a statically allocated string containing
47 * the ascii representation for number 'n' in decimal.
48 */
49char *ultoa(unsigned long n)
50{
51 char *pos;
52
53 pos = itoa_str + sizeof(itoa_str) - 1;
54 *pos-- = '\0';
55
56 do {
57 *pos-- = '0' + n % 10;
58 n /= 10;
59 } while (n && pos >= itoa_str);
60 return pos + 1;
61}
62
63
64/*
65 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
66 *
67 * It looks like this one would be a good candidate for inlining, but this is
68 * not interesting because it around 35 bytes long and often called multiple
69 * times within the same function.
70 */
71int ishex(char s)
72{
73 s -= '0';
74 if ((unsigned char)s <= 9)
75 return 1;
76 s -= 'A' - '0';
77 if ((unsigned char)s <= 5)
78 return 1;
79 s -= 'a' - 'A';
80 if ((unsigned char)s <= 5)
81 return 1;
82 return 0;
83}
84
85
86/*
87 * converts <str> to a struct sockaddr_in* which is locally allocated.
88 * The format is "addr:port", where "addr" can be a dotted IPv4 address,
89 * a host name, or empty or "*" to indicate INADDR_ANY.
90 */
91struct sockaddr_in *str2sa(char *str)
92{
93 static struct sockaddr_in sa;
94 char *c;
95 int port;
96
97 memset(&sa, 0, sizeof(sa));
98 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +020099 if (str == NULL)
100 goto out_nofree;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200101
102 if ((c = strrchr(str,':')) != NULL) {
103 *c++ = '\0';
104 port = atol(c);
105 }
106 else
107 port = 0;
108
109 if (*str == '*' || *str == '\0') { /* INADDR_ANY */
110 sa.sin_addr.s_addr = INADDR_ANY;
111 }
112 else if (!inet_pton(AF_INET, str, &sa.sin_addr)) {
113 struct hostent *he;
114
115 if ((he = gethostbyname(str)) == NULL) {
116 Alert("Invalid server name: '%s'\n", str);
117 }
118 else
119 sa.sin_addr = *(struct in_addr *) *(he->h_addr_list);
120 }
121 sa.sin_port = htons(port);
122 sa.sin_family = AF_INET;
123
124 free(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200125 out_nofree:
Willy Tarreaubaaee002006-06-26 02:48:02 +0200126 return &sa;
127}
128
129/*
130 * converts <str> to a two struct in_addr* which are locally allocated.
131 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
132 * is optionnal and either in the dotted or CIDR notation.
133 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
134 */
135int str2net(char *str, struct in_addr *addr, struct in_addr *mask)
136{
137 char *c;
138 unsigned long len;
139
140 memset(mask, 0, sizeof(*mask));
141 memset(addr, 0, sizeof(*addr));
142 str = strdup(str);
Willy Tarreauc6423482006-10-15 14:59:03 +0200143 if (str == NULL)
144 return 0;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200145
146 if ((c = strrchr(str, '/')) != NULL) {
147 *c++ = '\0';
148 /* c points to the mask */
149 if (strchr(c, '.') != NULL) { /* dotted notation */
150 if (!inet_pton(AF_INET, c, mask))
151 return 0;
152 }
153 else { /* mask length */
154 char *err;
155 len = strtol(c, &err, 10);
156 if (!*c || (err && *err) || (unsigned)len > 32)
157 return 0;
158 if (len)
159 mask->s_addr = htonl(~0UL << (32 - len));
160 else
161 mask->s_addr = 0;
162 }
163 }
164 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100165 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200166 }
167 if (!inet_pton(AF_INET, str, addr)) {
168 struct hostent *he;
169
170 if ((he = gethostbyname(str)) == NULL) {
171 return 0;
172 }
173 else
174 *addr = *(struct in_addr *) *(he->h_addr_list);
175 }
176 free(str);
177 return 1;
178}
179
180/* will try to encode the string <string> replacing all characters tagged in
181 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
182 * prefixed by <escape>, and will store the result between <start> (included)
183 * and <stop> (excluded), and will always terminate the string with a '\0'
184 * before <stop>. The position of the '\0' is returned if the conversion
185 * completes. If bytes are missing between <start> and <stop>, then the
186 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
187 * cannot even be stored so we return <start> without writing the 0.
188 * The input string must also be zero-terminated.
189 */
190const char hextab[16] = "0123456789ABCDEF";
191char *encode_string(char *start, char *stop,
192 const char escape, const fd_set *map,
193 const char *string)
194{
195 if (start < stop) {
196 stop--; /* reserve one byte for the final '\0' */
197 while (start < stop && *string != '\0') {
198 if (!FD_ISSET((unsigned char)(*string), map))
199 *start++ = *string;
200 else {
201 if (start + 3 >= stop)
202 break;
203 *start++ = escape;
204 *start++ = hextab[(*string >> 4) & 15];
205 *start++ = hextab[*string & 15];
206 }
207 string++;
208 }
209 *start = '\0';
210 }
211 return start;
212}
213
214
Willy Tarreau6911fa42007-03-04 18:06:08 +0100215unsigned int str2ui(const char *s)
216{
217 return __str2ui(s);
218}
219
220unsigned int str2uic(const char *s)
221{
222 return __str2uic(s);
223}
224
225unsigned int strl2ui(const char *s, int len)
226{
227 return __strl2ui(s, len);
228}
229
230unsigned int strl2uic(const char *s, int len)
231{
232 return __strl2uic(s, len);
233}
234
235/* This one is 7 times faster than strtol() on athlon with checks.
236 * It returns the value of the number composed of all valid digits read,
237 * and can process negative numbers too.
238 */
239int strl2ic(const char *s, int len)
240{
241 int i = 0;
242 int j;
243
244 if (len > 0) {
245 if (*s != '-') {
246 /* positive number */
247 while (len-- > 0) {
248 j = (*s++) - '0';
249 i = i * 10;
250 if (j > 9)
251 break;
252 i += j;
253 }
254 } else {
255 /* negative number */
256 s++;
257 while (--len > 0) {
258 j = (*s++) - '0';
259 i = i * 10;
260 if (j > 9)
261 break;
262 i -= j;
263 }
264 }
265 }
266 return i;
267}
268
269
270/* This function reads exactly <len> chars from <s> and converts them to a
271 * signed integer which it stores into <ret>. It accurately detects any error
272 * (truncated string, invalid chars, overflows). It is meant to be used in
273 * applications designed for hostile environments. It returns zero when the
274 * number has successfully been converted, non-zero otherwise. When an error
275 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
276 * faster than strtol().
277 */
278int strl2irc(const char *s, int len, int *ret)
279{
280 int i = 0;
281 int j;
282
283 if (!len)
284 return 1;
285
286 if (*s != '-') {
287 /* positive number */
288 while (len-- > 0) {
289 j = (*s++) - '0';
290 if (j > 9) return 1; /* invalid char */
291 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
292 i = i * 10;
293 if (i + j < i) return 1; /* check for addition overflow */
294 i = i + j;
295 }
296 } else {
297 /* negative number */
298 s++;
299 while (--len > 0) {
300 j = (*s++) - '0';
301 if (j > 9) return 1; /* invalid char */
302 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
303 i = i * 10;
304 if (i - j > i) return 1; /* check for subtract overflow */
305 i = i - j;
306 }
307 }
308 *ret = i;
309 return 0;
310}
311
312
313/* This function reads exactly <len> chars from <s> and converts them to a
314 * signed integer which it stores into <ret>. It accurately detects any error
315 * (truncated string, invalid chars, overflows). It is meant to be used in
316 * applications designed for hostile environments. It returns zero when the
317 * number has successfully been converted, non-zero otherwise. When an error
318 * is returned, the <ret> value is left untouched. It is about 3 times slower
319 * than str2irc().
320 */
321#ifndef LLONG_MAX
322#define LLONG_MAX 9223372036854775807LL
323#define LLONG_MIN (-LLONG_MAX - 1LL)
324#endif
325
326int strl2llrc(const char *s, int len, long long *ret)
327{
328 long long i = 0;
329 int j;
330
331 if (!len)
332 return 1;
333
334 if (*s != '-') {
335 /* positive number */
336 while (len-- > 0) {
337 j = (*s++) - '0';
338 if (j > 9) return 1; /* invalid char */
339 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
340 i = i * 10LL;
341 if (i + j < i) return 1; /* check for addition overflow */
342 i = i + j;
343 }
344 } else {
345 /* negative number */
346 s++;
347 while (--len > 0) {
348 j = (*s++) - '0';
349 if (j > 9) return 1; /* invalid char */
350 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
351 i = i * 10LL;
352 if (i - j > i) return 1; /* check for subtract overflow */
353 i = i - j;
354 }
355 }
356 *ret = i;
357 return 0;
358}
359
360
Willy Tarreaubaaee002006-06-26 02:48:02 +0200361/*
362 * Local variables:
363 * c-indent-level: 8
364 * c-basic-offset: 8
365 * End:
366 */