blob: adc66f52ff139403460e41be604bcb57e1350b28 [file] [log] [blame]
Willy Tarreaubaaee002006-06-26 02:48:02 +02001/*
2 * General purpose functions.
3 *
Willy Tarreau348238b2010-01-18 15:05:57 +01004 * Copyright 2000-2010 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>
Willy Tarreau127f9662007-12-06 00:53:51 +010017#include <sys/socket.h>
18#include <sys/un.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020019#include <netinet/in.h>
20#include <arpa/inet.h>
21
Willy Tarreaue3ba5f02006-06-29 18:54:54 +020022#include <common/config.h>
Willy Tarreau2dd0d472006-06-29 17:53:05 +020023#include <common/standard.h>
Willy Tarreau45cb4fb2009-10-26 21:10:04 +010024#include <eb32tree.h>
Willy Tarreaubaaee002006-06-26 02:48:02 +020025#include <proto/log.h>
26
Willy Tarreau72d759c2007-10-25 12:14:10 +020027/* enough to store 10 integers of :
28 * 2^64-1 = 18446744073709551615 or
29 * -2^63 = -9223372036854775808
Willy Tarreaue7239b52009-03-29 13:41:58 +020030 *
31 * The HTML version needs room for adding the 25 characters
32 * '<span class="rls"></span>' around digits at positions 3N+1 in order
33 * to add spacing at up to 6 positions : 18 446 744 073 709 551 615
Willy Tarreau72d759c2007-10-25 12:14:10 +020034 */
Willy Tarreaue7239b52009-03-29 13:41:58 +020035char itoa_str[10][171];
Willy Tarreaubaaee002006-06-26 02:48:02 +020036
37/*
38 * copies at most <size-1> chars from <src> to <dst>. Last char is always
39 * set to 0, unless <size> is 0. The number of chars copied is returned
40 * (excluding the terminating zero).
41 * This code has been optimized for size and speed : on x86, it's 45 bytes
42 * long, uses only registers, and consumes only 4 cycles per char.
43 */
44int strlcpy2(char *dst, const char *src, int size)
45{
46 char *orig = dst;
47 if (size) {
48 while (--size && (*dst = *src)) {
49 src++; dst++;
50 }
51 *dst = 0;
52 }
53 return dst - orig;
54}
55
56/*
Willy Tarreau72d759c2007-10-25 12:14:10 +020057 * This function simply returns a locally allocated string containing
Willy Tarreaubaaee002006-06-26 02:48:02 +020058 * the ascii representation for number 'n' in decimal.
59 */
Emeric Brun3a7fce52010-01-04 14:54:38 +010060char *ultoa_r(unsigned long n, char *buffer, int size)
Willy Tarreaubaaee002006-06-26 02:48:02 +020061{
62 char *pos;
63
Willy Tarreau72d759c2007-10-25 12:14:10 +020064 pos = buffer + size - 1;
Willy Tarreaubaaee002006-06-26 02:48:02 +020065 *pos-- = '\0';
66
67 do {
68 *pos-- = '0' + n % 10;
69 n /= 10;
Willy Tarreau72d759c2007-10-25 12:14:10 +020070 } while (n && pos >= buffer);
Willy Tarreaubaaee002006-06-26 02:48:02 +020071 return pos + 1;
72}
73
Willy Tarreau91092e52007-10-25 16:58:42 +020074/*
Willy Tarreaue7239b52009-03-29 13:41:58 +020075 * This function simply returns a locally allocated string containing
76 * the ascii representation for number 'n' in decimal, formatted for
77 * HTML output with tags to create visual grouping by 3 digits. The
78 * output needs to support at least 171 characters.
79 */
80const char *ulltoh_r(unsigned long long n, char *buffer, int size)
81{
82 char *start;
83 int digit = 0;
84
85 start = buffer + size;
86 *--start = '\0';
87
88 do {
89 if (digit == 3 && start >= buffer + 7)
90 memcpy(start -= 7, "</span>", 7);
91
92 if (start >= buffer + 1) {
93 *--start = '0' + n % 10;
94 n /= 10;
95 }
96
97 if (digit == 3 && start >= buffer + 18)
98 memcpy(start -= 18, "<span class=\"rls\">", 18);
99
100 if (digit++ == 3)
101 digit = 1;
102 } while (n && start > buffer);
103 return start;
104}
105
106/*
Willy Tarreau91092e52007-10-25 16:58:42 +0200107 * This function simply returns a locally allocated string containing the ascii
108 * representation for number 'n' in decimal, unless n is 0 in which case it
109 * returns the alternate string (or an empty string if the alternate string is
110 * NULL). It use is intended for limits reported in reports, where it's
111 * desirable not to display anything if there is no limit. Warning! it shares
112 * the same vector as ultoa_r().
113 */
114const char *limit_r(unsigned long n, char *buffer, int size, const char *alt)
115{
116 return (n) ? ultoa_r(n, buffer, size) : (alt ? alt : "");
117}
118
Robert Tsai81ae1952007-12-05 10:47:29 +0100119/*
120 * converts <str> to a struct sockaddr_un* which is locally allocated.
121 * The format is "/path", where "/path" is a path to a UNIX domain socket.
Willy Tarreaud5191e72010-02-09 20:50:45 +0100122 * NULL is returned if the socket path is invalid (too long).
Robert Tsai81ae1952007-12-05 10:47:29 +0100123 */
Willy Tarreaucaf720d2008-03-07 10:07:04 +0100124struct sockaddr_un *str2sun(const char *str)
Robert Tsai81ae1952007-12-05 10:47:29 +0100125{
Willy Tarreau127f9662007-12-06 00:53:51 +0100126 static struct sockaddr_un su;
Robert Tsai81ae1952007-12-05 10:47:29 +0100127 int strsz; /* length included null */
128
Willy Tarreau127f9662007-12-06 00:53:51 +0100129 memset(&su, 0, sizeof(su));
Robert Tsai81ae1952007-12-05 10:47:29 +0100130 strsz = strlen(str) + 1;
Willy Tarreau127f9662007-12-06 00:53:51 +0100131 if (strsz > sizeof(su.sun_path)) {
Willy Tarreaud5191e72010-02-09 20:50:45 +0100132 return NULL;
Willy Tarreaucaf720d2008-03-07 10:07:04 +0100133 } else {
134 su.sun_family = AF_UNIX;
135 memcpy(su.sun_path, str, strsz);
Robert Tsai81ae1952007-12-05 10:47:29 +0100136 }
Willy Tarreau127f9662007-12-06 00:53:51 +0100137 return &su;
Robert Tsai81ae1952007-12-05 10:47:29 +0100138}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200139
140/*
141 * Returns non-zero if character <s> is a hex digit (0-9, a-f, A-F), else zero.
142 *
143 * It looks like this one would be a good candidate for inlining, but this is
144 * not interesting because it around 35 bytes long and often called multiple
145 * times within the same function.
146 */
147int ishex(char s)
148{
149 s -= '0';
150 if ((unsigned char)s <= 9)
151 return 1;
152 s -= 'A' - '0';
153 if ((unsigned char)s <= 5)
154 return 1;
155 s -= 'a' - 'A';
156 if ((unsigned char)s <= 5)
157 return 1;
158 return 0;
159}
160
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100161/*
Willy Tarreauda3b7c32009-11-02 20:12:52 +0100162 * Return integer equivalent of character <c> for a hex digit (0-9, a-f, A-F),
163 * otherwise -1. This compact form helps gcc produce efficient code.
164 */
165int hex2i(int c)
166{
167 if ((unsigned char)(c -= '0') > 9) {
168 if ((unsigned char)(c -= 'A' - '0') > 5 &&
169 (unsigned char)(c -= 'a' - 'A') > 5)
170 c = -11;
171 c += 10;
172 }
173 return c;
174}
175
176/*
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100177 * Checks <name> for invalid characters. Valid chars are [A-Za-z0-9_:.-]. If an
178 * invalid character is found, a pointer to it is returned. If everything is
179 * fine, NULL is returned.
180 */
181const char *invalid_char(const char *name)
182{
183 if (!*name)
184 return name;
185
186 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100187 if (!isalnum((int)(unsigned char)*name) && *name != '.' && *name != ':' &&
Willy Tarreau2e74c3f2007-12-02 18:45:09 +0100188 *name != '_' && *name != '-')
189 return name;
190 name++;
191 }
192 return NULL;
193}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200194
195/*
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200196 * Checks <domainname> for invalid characters. Valid chars are [A-Za-z0-9_.-].
197 * If an invalid character is found, a pointer to it is returned.
198 * If everything is fine, NULL is returned.
199 */
200const char *invalid_domainchar(const char *name) {
201
202 if (!*name)
203 return name;
204
205 while (*name) {
Willy Tarreau88e05812010-03-03 00:16:00 +0100206 if (!isalnum((int)(unsigned char)*name) && *name != '.' &&
Krzysztof Piotr Oledzkiefe3b6f2008-05-23 23:49:32 +0200207 *name != '_' && *name != '-')
208 return name;
209
210 name++;
211 }
212
213 return NULL;
214}
215
216/*
Willy Tarreaufab5a432011-03-04 15:31:53 +0100217 * converts <str> to a struct sockaddr_storage* which is locally allocated. The
218 * string is assumed to contain only an address, no port. The address can be a
219 * dotted IPv4 address, an IPv6 address, a host name, or empty or "*" to
220 * indicate INADDR_ANY. NULL is returned if the host part cannot be resolved.
221 * The return address will only have the address family and the address set,
222 * all other fields remain zero. The string is not supposed to be modified.
223 * The IPv6 '::' address is IN6ADDR_ANY.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200224 */
Willy Tarreaufab5a432011-03-04 15:31:53 +0100225struct sockaddr_storage *str2ip(const char *str)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200226{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100227 static struct sockaddr_storage sa;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100228 struct hostent *he;
229
230 memset(&sa, 0, sizeof(sa));
231
232 /* Any IPv6 address */
233 if (str[0] == ':' && str[1] == ':' && !str[2]) {
234 sa.ss_family = AF_INET6;
235 return &sa;
236 }
237
238 /* Any IPv4 address */
239 if (!str[0] || (str[0] == '*' && !str[1])) {
240 sa.ss_family = AF_INET;
241 return &sa;
242 }
243
244 /* check for IPv6 first */
245 if (inet_pton(AF_INET6, str, &((struct sockaddr_in6 *)&sa)->sin6_addr)) {
246 sa.ss_family = AF_INET6;
247 return &sa;
248 }
249
250 /* then check for IPv4 */
251 if (inet_pton(AF_INET, str, &((struct sockaddr_in *)&sa)->sin_addr)) {
252 sa.ss_family = AF_INET;
253 return &sa;
254 }
255
256 /* try to resolve an IPv4/IPv6 hostname */
257 he = gethostbyname(str);
258 if (he) {
259 sa.ss_family = he->h_addrtype;
260 switch (sa.ss_family) {
261 case AF_INET:
262 ((struct sockaddr_in *)&sa)->sin_addr = *(struct in_addr *) *(he->h_addr_list);
263 return &sa;
264 case AF_INET6:
265 ((struct sockaddr_in6 *)&sa)->sin6_addr = *(struct in6_addr *) *(he->h_addr_list);
266 return &sa;
267 }
David du Colombierd5f43282011-03-17 10:40:16 +0100268 }
269#ifdef USE_GETADDRINFO
270 else {
271 struct addrinfo hints, *result;
272
273 memset(&result, 0, sizeof(result));
274 memset(&hints, 0, sizeof(hints));
275 hints.ai_family = AF_UNSPEC;
276 hints.ai_socktype = SOCK_DGRAM;
277 hints.ai_flags = AI_PASSIVE;
278 hints.ai_protocol = 0;
279
280 if (getaddrinfo(str, NULL, &hints, &result) == 0) {
281 sa.ss_family = result->ai_family;
282 switch (result->ai_family) {
283 case AF_INET:
284 memcpy((struct sockaddr_in *)&sa, result->ai_addr, result->ai_addrlen);
285 return &sa;
286 case AF_INET6:
287 memcpy((struct sockaddr_in6 *)&sa, result->ai_addr, result->ai_addrlen);
288 return &sa;
289 }
290 }
291
292 freeaddrinfo(result);
Willy Tarreaufab5a432011-03-04 15:31:53 +0100293 }
David du Colombierd5f43282011-03-17 10:40:16 +0100294#endif
295 /* unsupported address family */
Willy Tarreaufab5a432011-03-04 15:31:53 +0100296
297 return NULL;
298}
299
300/*
301 * converts <str> to a locally allocated struct sockaddr_storage *.
302 * The format is "addr[:[port]]", where "addr" can be a dotted IPv4 address, an
303 * IPv6 address, a host name, or empty or "*" to indicate INADDR_ANY. If an IPv6
304 * address wants to ignore port, it must be terminated by a trailing colon (':').
305 * The IPv6 '::' address is IN6ADDR_ANY, so in order to bind to a given port on
306 * IPv6, use ":::port". NULL is returned if the host part cannot be resolved.
307 */
308struct sockaddr_storage *str2sa(const char *str)
309{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100310 struct sockaddr_storage *ret = NULL;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100311 char *str2;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200312 char *c;
313 int port;
314
Willy Tarreaufab5a432011-03-04 15:31:53 +0100315 str2 = strdup(str);
316 if (str2 == NULL)
Willy Tarreaud5191e72010-02-09 20:50:45 +0100317 goto out;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200318
Willy Tarreaufab5a432011-03-04 15:31:53 +0100319 if ((c = strrchr(str2, ':')) != NULL) { /* Port */
Willy Tarreaubaaee002006-06-26 02:48:02 +0200320 *c++ = '\0';
321 port = atol(c);
322 }
323 else
324 port = 0;
325
Willy Tarreaufab5a432011-03-04 15:31:53 +0100326 ret = str2ip(str2);
327 if (!ret)
328 goto out;
329
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200330 set_host_port(ret, port);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100331 out:
Willy Tarreaufab5a432011-03-04 15:31:53 +0100332 free(str2);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100333 return ret;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200334}
335
336/*
Willy Tarreaufab5a432011-03-04 15:31:53 +0100337 * converts <str> to a locally allocated struct sockaddr_storage *, and a
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200338 * port range consisting in two integers. The low and high end are always set
339 * even if the port is unspecified, in which case (0,0) is returned. The low
Willy Tarreaufab5a432011-03-04 15:31:53 +0100340 * port is set in the sockaddr. Thus, it is enough to check the size of the
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200341 * returned range to know if an array must be allocated or not. The format is
Willy Tarreaufab5a432011-03-04 15:31:53 +0100342 * "addr[:[port[-port]]]", where "addr" can be a dotted IPv4 address, an IPv6
343 * address, a host name, or empty or "*" to indicate INADDR_ANY. If an IPv6
344 * address wants to ignore port, it must be terminated by a trailing colon (':').
345 * The IPv6 '::' address is IN6ADDR_ANY, so in order to bind to a given port on
346 * IPv6, use ":::port". NULL is returned if the host part cannot be resolved.
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200347 */
Willy Tarreaufab5a432011-03-04 15:31:53 +0100348struct sockaddr_storage *str2sa_range(const char *str, int *low, int *high)
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200349{
David du Colombier6f5ccb12011-03-10 22:26:24 +0100350 struct sockaddr_storage *ret = NULL;
Willy Tarreaufab5a432011-03-04 15:31:53 +0100351 char *str2;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200352 char *c;
353 int portl, porth;
354
Willy Tarreaufab5a432011-03-04 15:31:53 +0100355 str2 = strdup(str);
356 if (str2 == NULL)
Willy Tarreaud5191e72010-02-09 20:50:45 +0100357 goto out;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200358
Willy Tarreaufab5a432011-03-04 15:31:53 +0100359 if ((c = strrchr(str2,':')) != NULL) { /* Port */
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200360 char *sep;
361 *c++ = '\0';
362 sep = strchr(c, '-');
363 if (sep)
364 *sep++ = '\0';
365 else
366 sep = c;
367 portl = atol(c);
368 porth = atol(sep);
369 }
370 else {
371 portl = 0;
372 porth = 0;
373 }
374
Willy Tarreaufab5a432011-03-04 15:31:53 +0100375 ret = str2ip(str2);
376 if (!ret)
377 goto out;
378
Willy Tarreau86ad42c2011-08-27 12:29:07 +0200379 set_host_port(ret, portl);
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200380
381 *low = portl;
382 *high = porth;
Willy Tarreaud5191e72010-02-09 20:50:45 +0100383 out:
Willy Tarreaufab5a432011-03-04 15:31:53 +0100384 free(str2);
Willy Tarreaud5191e72010-02-09 20:50:45 +0100385 return ret;
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200386}
387
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100388/* converts <str> to a struct in_addr containing a network mask. It can be
389 * passed in dotted form (255.255.255.0) or in CIDR form (24). It returns 1
390 * if the conversion succeeds otherwise non-zero.
391 */
392int str2mask(const char *str, struct in_addr *mask)
393{
394 if (strchr(str, '.') != NULL) { /* dotted notation */
395 if (!inet_pton(AF_INET, str, mask))
396 return 0;
397 }
398 else { /* mask length */
399 char *err;
400 unsigned long len = strtol(str, &err, 10);
401
402 if (!*str || (err && *err) || (unsigned)len > 32)
403 return 0;
404 if (len)
405 mask->s_addr = htonl(~0UL << (32 - len));
406 else
407 mask->s_addr = 0;
408 }
409 return 1;
410}
411
Willy Tarreauc6f4ce82009-06-10 11:09:37 +0200412/*
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200413 * converts <str> to two struct in_addr* which must be pre-allocated.
Willy Tarreaubaaee002006-06-26 02:48:02 +0200414 * The format is "addr[/mask]", where "addr" cannot be empty, and mask
415 * is optionnal and either in the dotted or CIDR notation.
416 * Note: "addr" can also be a hostname. Returns 1 if OK, 0 if error.
417 */
Willy Tarreaud077a8e2007-05-08 18:28:09 +0200418int str2net(const char *str, struct in_addr *addr, struct in_addr *mask)
Willy Tarreaubaaee002006-06-26 02:48:02 +0200419{
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200420 __label__ out_free, out_err;
421 char *c, *s;
422 int ret_val;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200423
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200424 s = strdup(str);
425 if (!s)
426 return 0;
427
Willy Tarreaubaaee002006-06-26 02:48:02 +0200428 memset(mask, 0, sizeof(*mask));
429 memset(addr, 0, sizeof(*addr));
Willy Tarreaubaaee002006-06-26 02:48:02 +0200430
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200431 if ((c = strrchr(s, '/')) != NULL) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200432 *c++ = '\0';
433 /* c points to the mask */
Willy Tarreau2937c0d2010-01-26 17:36:17 +0100434 if (!str2mask(c, mask))
435 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200436 }
437 else {
Willy Tarreauebd61602006-12-30 11:54:15 +0100438 mask->s_addr = ~0U;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200439 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200440 if (!inet_pton(AF_INET, s, addr)) {
Willy Tarreaubaaee002006-06-26 02:48:02 +0200441 struct hostent *he;
442
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200443 if ((he = gethostbyname(s)) == NULL) {
444 goto out_err;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200445 }
446 else
447 *addr = *(struct in_addr *) *(he->h_addr_list);
448 }
Willy Tarreau8aeae4a2007-06-17 11:42:08 +0200449
450 ret_val = 1;
451 out_free:
452 free(s);
453 return ret_val;
454 out_err:
455 ret_val = 0;
456 goto out_free;
Willy Tarreaubaaee002006-06-26 02:48:02 +0200457}
458
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100459
460/*
David du Colombier6f5ccb12011-03-10 22:26:24 +0100461 * Parse IPv4 address found in url.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100462 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100463int url2ipv4(const char *addr, struct in_addr *dst)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100464{
465 int saw_digit, octets, ch;
466 u_char tmp[4], *tp;
467 const char *cp = addr;
468
469 saw_digit = 0;
470 octets = 0;
471 *(tp = tmp) = 0;
472
473 while (*addr) {
474 unsigned char digit = (ch = *addr++) - '0';
475 if (digit > 9 && ch != '.')
476 break;
477 if (digit <= 9) {
478 u_int new = *tp * 10 + digit;
479 if (new > 255)
480 return 0;
481 *tp = new;
482 if (!saw_digit) {
483 if (++octets > 4)
484 return 0;
485 saw_digit = 1;
486 }
487 } else if (ch == '.' && saw_digit) {
488 if (octets == 4)
489 return 0;
490 *++tp = 0;
491 saw_digit = 0;
492 } else
493 return 0;
494 }
495
496 if (octets < 4)
497 return 0;
498
499 memcpy(&dst->s_addr, tmp, 4);
500 return addr-cp-1;
501}
502
503/*
David du Colombier6f5ccb12011-03-10 22:26:24 +0100504 * Resolve destination server from URL. Convert <str> to a sockaddr_storage*.
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100505 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100506int url2sa(const char *url, int ulen, struct sockaddr_storage *addr)
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100507{
508 const char *curr = url, *cp = url;
509 int ret, url_code = 0;
510 unsigned int http_code = 0;
511
512 /* Cleanup the room */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100513
514 /* FIXME: assume IPv4 only for now */
515 ((struct sockaddr_in *)addr)->sin_family = AF_INET;
516 ((struct sockaddr_in *)addr)->sin_addr.s_addr = 0;
517 ((struct sockaddr_in *)addr)->sin_port = 0;
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100518
519 /* Firstly, try to find :// pattern */
520 while (curr < url+ulen && url_code != 0x3a2f2f) {
521 url_code = ((url_code & 0xffff) << 8);
522 url_code += (unsigned char)*curr++;
523 }
524
525 /* Secondly, if :// pattern is found, verify parsed stuff
526 * before pattern is matching our http pattern.
527 * If so parse ip address and port in uri.
528 *
529 * WARNING: Current code doesn't support dynamic async dns resolver.
530 */
531 if (url_code == 0x3a2f2f) {
532 while (cp < curr - 3)
533 http_code = (http_code << 8) + *cp++;
534 http_code |= 0x20202020; /* Turn everything to lower case */
535
536 /* HTTP url matching */
537 if (http_code == 0x68747470) {
538 /* We are looking for IP address. If you want to parse and
539 * resolve hostname found in url, you can use str2sa(), but
540 * be warned this can slow down global daemon performances
541 * while handling lagging dns responses.
542 */
David du Colombier6f5ccb12011-03-10 22:26:24 +0100543 ret = url2ipv4(curr, &((struct sockaddr_in *)&addr)->sin_addr);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100544 if (!ret)
545 return -1;
546 curr += ret;
David du Colombier6f5ccb12011-03-10 22:26:24 +0100547 ((struct sockaddr_in *)addr)->sin_port = (*curr == ':') ? str2uic(++curr) : 80;
548 ((struct sockaddr_in *)addr)->sin_port = htons(((struct sockaddr_in *)&addr)->sin_port);
Alexandre Cassen5eb1a902007-11-29 15:43:32 +0100549 }
550 return 0;
551 }
552
553 return -1;
554}
555
Willy Tarreau631f01c2011-09-05 00:36:48 +0200556/* Tries to convert a sockaddr_storage address to text form. Upon success, the
557 * address family is returned so that it's easy for the caller to adapt to the
558 * output format. Zero is returned if the address family is not supported. -1
559 * is returned upon error, with errno set. AF_INET, AF_INET6 and AF_UNIX are
560 * supported.
561 */
562int addr_to_str(struct sockaddr_storage *addr, char *str, int size)
563{
564
565 void *ptr;
566
567 if (size < 5)
568 return 0;
569 *str = '\0';
570
571 switch (addr->ss_family) {
572 case AF_INET:
573 ptr = &((struct sockaddr_in *)addr)->sin_addr;
574 break;
575 case AF_INET6:
576 ptr = &((struct sockaddr_in6 *)addr)->sin6_addr;
577 break;
578 case AF_UNIX:
579 memcpy(str, "unix", 5);
580 return addr->ss_family;
581 default:
582 return 0;
583 }
584
585 if (inet_ntop(addr->ss_family, ptr, str, size))
586 return addr->ss_family;
587
588 /* failed */
589 return -1;
590}
591
Willy Tarreaubaaee002006-06-26 02:48:02 +0200592/* will try to encode the string <string> replacing all characters tagged in
593 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
594 * prefixed by <escape>, and will store the result between <start> (included)
595 * and <stop> (excluded), and will always terminate the string with a '\0'
596 * before <stop>. The position of the '\0' is returned if the conversion
597 * completes. If bytes are missing between <start> and <stop>, then the
598 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
599 * cannot even be stored so we return <start> without writing the 0.
600 * The input string must also be zero-terminated.
601 */
602const char hextab[16] = "0123456789ABCDEF";
603char *encode_string(char *start, char *stop,
604 const char escape, const fd_set *map,
605 const char *string)
606{
607 if (start < stop) {
608 stop--; /* reserve one byte for the final '\0' */
609 while (start < stop && *string != '\0') {
610 if (!FD_ISSET((unsigned char)(*string), map))
611 *start++ = *string;
612 else {
613 if (start + 3 >= stop)
614 break;
615 *start++ = escape;
616 *start++ = hextab[(*string >> 4) & 15];
617 *start++ = hextab[*string & 15];
618 }
619 string++;
620 }
621 *start = '\0';
622 }
623 return start;
624}
625
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +0200626/* Decode an URL-encoded string in-place. The resulting string might
627 * be shorter. If some forbidden characters are found, the conversion is
628 * aborted, the string is truncated before the issue and non-zero is returned,
629 * otherwise the operation returns non-zero indicating success.
630 */
631int url_decode(char *string)
632{
633 char *in, *out;
634 int ret = 0;
635
636 in = string;
637 out = string;
638 while (*in) {
639 switch (*in) {
640 case '+' :
641 *out++ = ' ';
642 break;
643 case '%' :
644 if (!ishex(in[1]) || !ishex(in[2]))
645 goto end;
646 *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]);
647 in += 2;
648 break;
649 default:
650 *out++ = *in;
651 break;
652 }
653 in++;
654 }
655 ret = 1; /* success */
656 end:
657 *out = 0;
658 return ret;
659}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200660
Willy Tarreau6911fa42007-03-04 18:06:08 +0100661unsigned int str2ui(const char *s)
662{
663 return __str2ui(s);
664}
665
666unsigned int str2uic(const char *s)
667{
668 return __str2uic(s);
669}
670
671unsigned int strl2ui(const char *s, int len)
672{
673 return __strl2ui(s, len);
674}
675
676unsigned int strl2uic(const char *s, int len)
677{
678 return __strl2uic(s, len);
679}
680
Willy Tarreau4ec83cd2010-10-15 23:19:55 +0200681unsigned int read_uint(const char **s, const char *end)
682{
683 return __read_uint(s, end);
684}
685
Willy Tarreau6911fa42007-03-04 18:06:08 +0100686/* This one is 7 times faster than strtol() on athlon with checks.
687 * It returns the value of the number composed of all valid digits read,
688 * and can process negative numbers too.
689 */
690int strl2ic(const char *s, int len)
691{
692 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200693 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100694
695 if (len > 0) {
696 if (*s != '-') {
697 /* positive number */
698 while (len-- > 0) {
699 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200700 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100701 if (j > 9)
702 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200703 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100704 }
705 } else {
706 /* negative number */
707 s++;
708 while (--len > 0) {
709 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200710 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100711 if (j > 9)
712 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200713 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100714 }
715 }
716 }
717 return i;
718}
719
720
721/* This function reads exactly <len> chars from <s> and converts them to a
722 * signed integer which it stores into <ret>. It accurately detects any error
723 * (truncated string, invalid chars, overflows). It is meant to be used in
724 * applications designed for hostile environments. It returns zero when the
725 * number has successfully been converted, non-zero otherwise. When an error
726 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
727 * faster than strtol().
728 */
729int strl2irc(const char *s, int len, int *ret)
730{
731 int i = 0;
732 int j;
733
734 if (!len)
735 return 1;
736
737 if (*s != '-') {
738 /* positive number */
739 while (len-- > 0) {
740 j = (*s++) - '0';
741 if (j > 9) return 1; /* invalid char */
742 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
743 i = i * 10;
744 if (i + j < i) return 1; /* check for addition overflow */
745 i = i + j;
746 }
747 } else {
748 /* negative number */
749 s++;
750 while (--len > 0) {
751 j = (*s++) - '0';
752 if (j > 9) return 1; /* invalid char */
753 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
754 i = i * 10;
755 if (i - j > i) return 1; /* check for subtract overflow */
756 i = i - j;
757 }
758 }
759 *ret = i;
760 return 0;
761}
762
763
764/* This function reads exactly <len> chars from <s> and converts them to a
765 * signed integer which it stores into <ret>. It accurately detects any error
766 * (truncated string, invalid chars, overflows). It is meant to be used in
767 * applications designed for hostile environments. It returns zero when the
768 * number has successfully been converted, non-zero otherwise. When an error
769 * is returned, the <ret> value is left untouched. It is about 3 times slower
770 * than str2irc().
771 */
772#ifndef LLONG_MAX
773#define LLONG_MAX 9223372036854775807LL
774#define LLONG_MIN (-LLONG_MAX - 1LL)
775#endif
776
777int strl2llrc(const char *s, int len, long long *ret)
778{
779 long long i = 0;
780 int j;
781
782 if (!len)
783 return 1;
784
785 if (*s != '-') {
786 /* positive number */
787 while (len-- > 0) {
788 j = (*s++) - '0';
789 if (j > 9) return 1; /* invalid char */
790 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
791 i = i * 10LL;
792 if (i + j < i) return 1; /* check for addition overflow */
793 i = i + j;
794 }
795 } else {
796 /* negative number */
797 s++;
798 while (--len > 0) {
799 j = (*s++) - '0';
800 if (j > 9) return 1; /* invalid char */
801 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
802 i = i * 10LL;
803 if (i - j > i) return 1; /* check for subtract overflow */
804 i = i - j;
805 }
806 }
807 *ret = i;
808 return 0;
809}
810
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100811/* This function parses a time value optionally followed by a unit suffix among
812 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
813 * expected by the caller. The computation does its best to avoid overflows.
814 * The value is returned in <ret> if everything is fine, and a NULL is returned
815 * by the function. In case of error, a pointer to the error is returned and
816 * <ret> is left untouched. Values are automatically rounded up when needed.
817 */
818const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
819{
820 unsigned imult, idiv;
821 unsigned omult, odiv;
822 unsigned value;
823
824 omult = odiv = 1;
825
826 switch (unit_flags & TIME_UNIT_MASK) {
827 case TIME_UNIT_US: omult = 1000000; break;
828 case TIME_UNIT_MS: omult = 1000; break;
829 case TIME_UNIT_S: break;
830 case TIME_UNIT_MIN: odiv = 60; break;
831 case TIME_UNIT_HOUR: odiv = 3600; break;
832 case TIME_UNIT_DAY: odiv = 86400; break;
833 default: break;
834 }
835
836 value = 0;
837
838 while (1) {
839 unsigned int j;
840
841 j = *text - '0';
842 if (j > 9)
843 break;
844 text++;
845 value *= 10;
846 value += j;
847 }
848
849 imult = idiv = 1;
850 switch (*text) {
851 case '\0': /* no unit = default unit */
852 imult = omult = idiv = odiv = 1;
853 break;
854 case 's': /* second = unscaled unit */
855 break;
856 case 'u': /* microsecond : "us" */
857 if (text[1] == 's') {
858 idiv = 1000000;
859 text++;
860 }
861 break;
862 case 'm': /* millisecond : "ms" or minute: "m" */
863 if (text[1] == 's') {
864 idiv = 1000;
865 text++;
866 } else
867 imult = 60;
868 break;
869 case 'h': /* hour : "h" */
870 imult = 3600;
871 break;
872 case 'd': /* day : "d" */
873 imult = 86400;
874 break;
875 default:
876 return text;
877 break;
878 }
879
880 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
881 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
882 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
883 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
884
885 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
886 *ret = value;
887 return NULL;
888}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100889
Emeric Brun39132b22010-01-04 14:57:24 +0100890/* this function converts the string starting at <text> to an unsigned int
891 * stored in <ret>. If an error is detected, the pointer to the unexpected
892 * character is returned. If the conversio is succesful, NULL is returned.
893 */
894const char *parse_size_err(const char *text, unsigned *ret) {
895 unsigned value = 0;
896
897 while (1) {
898 unsigned int j;
899
900 j = *text - '0';
901 if (j > 9)
902 break;
903 if (value > ~0U / 10)
904 return text;
905 value *= 10;
906 if (value > (value + j))
907 return text;
908 value += j;
909 text++;
910 }
911
912 switch (*text) {
913 case '\0':
914 break;
915 case 'K':
916 case 'k':
917 if (value > ~0U >> 10)
918 return text;
919 value = value << 10;
920 break;
921 case 'M':
922 case 'm':
923 if (value > ~0U >> 20)
924 return text;
925 value = value << 20;
926 break;
927 case 'G':
928 case 'g':
929 if (value > ~0U >> 30)
930 return text;
931 value = value << 30;
932 break;
933 default:
934 return text;
935 }
936
937 *ret = value;
938 return NULL;
939}
940
Willy Tarreau946ba592009-05-10 15:41:18 +0200941/* copies at most <n> characters from <src> and always terminates with '\0' */
942char *my_strndup(const char *src, int n)
943{
944 int len = 0;
945 char *ret;
946
947 while (len < n && src[len])
948 len++;
949
950 ret = (char *)malloc(len + 1);
951 if (!ret)
952 return ret;
953 memcpy(ret, src, len);
954 ret[len] = '\0';
955 return ret;
956}
957
Willy Tarreau482b00d2009-10-04 22:48:42 +0200958/* This function returns the first unused key greater than or equal to <key> in
959 * ID tree <root>. Zero is returned if no place is found.
960 */
961unsigned int get_next_id(struct eb_root *root, unsigned int key)
962{
963 struct eb32_node *used;
964
965 do {
966 used = eb32_lookup_ge(root, key);
967 if (!used || used->key > key)
968 return key; /* key is available */
969 key++;
970 } while (key);
971 return key;
972}
973
Willy Tarreau348238b2010-01-18 15:05:57 +0100974/* This function compares a sample word possibly followed by blanks to another
975 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
976 * otherwise zero. This intends to be used when checking HTTP headers for some
977 * values. Note that it validates a word followed only by blanks but does not
978 * validate a word followed by blanks then other chars.
979 */
980int word_match(const char *sample, int slen, const char *word, int wlen)
981{
982 if (slen < wlen)
983 return 0;
984
985 while (wlen) {
986 char c = *sample ^ *word;
987 if (c && c != ('A' ^ 'a'))
988 return 0;
989 sample++;
990 word++;
991 slen--;
992 wlen--;
993 }
994
995 while (slen) {
996 if (*sample != ' ' && *sample != '\t')
997 return 0;
998 sample++;
999 slen--;
1000 }
1001 return 1;
1002}
Willy Tarreau482b00d2009-10-04 22:48:42 +02001003
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001004/* Converts any text-formatted IPv4 address to a host-order IPv4 address. It
1005 * is particularly fast because it avoids expensive operations such as
1006 * multiplies, which are optimized away at the end. It requires a properly
1007 * formated address though (3 points).
1008 */
1009unsigned int inetaddr_host(const char *text)
1010{
1011 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1012 register unsigned int dig100, dig10, dig1;
1013 int s;
1014 const char *p, *d;
1015
1016 dig1 = dig10 = dig100 = ascii_zero;
1017 s = 24;
1018
1019 p = text;
1020 while (1) {
1021 if (((unsigned)(*p - '0')) <= 9) {
1022 p++;
1023 continue;
1024 }
1025
1026 /* here, we have a complete byte between <text> and <p> (exclusive) */
1027 if (p == text)
1028 goto end;
1029
1030 d = p - 1;
1031 dig1 |= (unsigned int)(*d << s);
1032 if (d == text)
1033 goto end;
1034
1035 d--;
1036 dig10 |= (unsigned int)(*d << s);
1037 if (d == text)
1038 goto end;
1039
1040 d--;
1041 dig100 |= (unsigned int)(*d << s);
1042 end:
1043 if (!s || *p != '.')
1044 break;
1045
1046 s -= 8;
1047 text = ++p;
1048 }
1049
1050 dig100 -= ascii_zero;
1051 dig10 -= ascii_zero;
1052 dig1 -= ascii_zero;
1053 return ((dig100 * 10) + dig10) * 10 + dig1;
1054}
1055
1056/*
1057 * Idem except the first unparsed character has to be passed in <stop>.
1058 */
1059unsigned int inetaddr_host_lim(const char *text, const char *stop)
1060{
1061 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1062 register unsigned int dig100, dig10, dig1;
1063 int s;
1064 const char *p, *d;
1065
1066 dig1 = dig10 = dig100 = ascii_zero;
1067 s = 24;
1068
1069 p = text;
1070 while (1) {
1071 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1072 p++;
1073 continue;
1074 }
1075
1076 /* here, we have a complete byte between <text> and <p> (exclusive) */
1077 if (p == text)
1078 goto end;
1079
1080 d = p - 1;
1081 dig1 |= (unsigned int)(*d << s);
1082 if (d == text)
1083 goto end;
1084
1085 d--;
1086 dig10 |= (unsigned int)(*d << s);
1087 if (d == text)
1088 goto end;
1089
1090 d--;
1091 dig100 |= (unsigned int)(*d << s);
1092 end:
1093 if (!s || p == stop || *p != '.')
1094 break;
1095
1096 s -= 8;
1097 text = ++p;
1098 }
1099
1100 dig100 -= ascii_zero;
1101 dig10 -= ascii_zero;
1102 dig1 -= ascii_zero;
1103 return ((dig100 * 10) + dig10) * 10 + dig1;
1104}
1105
1106/*
1107 * Idem except the pointer to first unparsed byte is returned into <ret> which
1108 * must not be NULL.
1109 */
Willy Tarreau74172752010-10-15 23:21:42 +02001110unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret)
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001111{
1112 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1113 register unsigned int dig100, dig10, dig1;
1114 int s;
Willy Tarreau74172752010-10-15 23:21:42 +02001115 char *p, *d;
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001116
1117 dig1 = dig10 = dig100 = ascii_zero;
1118 s = 24;
1119
1120 p = text;
1121 while (1) {
1122 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1123 p++;
1124 continue;
1125 }
1126
1127 /* here, we have a complete byte between <text> and <p> (exclusive) */
1128 if (p == text)
1129 goto end;
1130
1131 d = p - 1;
1132 dig1 |= (unsigned int)(*d << s);
1133 if (d == text)
1134 goto end;
1135
1136 d--;
1137 dig10 |= (unsigned int)(*d << s);
1138 if (d == text)
1139 goto end;
1140
1141 d--;
1142 dig100 |= (unsigned int)(*d << s);
1143 end:
1144 if (!s || p == stop || *p != '.')
1145 break;
1146
1147 s -= 8;
1148 text = ++p;
1149 }
1150
1151 *ret = p;
1152 dig100 -= ascii_zero;
1153 dig10 -= ascii_zero;
1154 dig1 -= ascii_zero;
1155 return ((dig100 * 10) + dig10) * 10 + dig1;
1156}
1157
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001158/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
1159 * or the number of chars read in case of success. Maybe this could be replaced
1160 * by one of the functions above. Also, apparently this function does not support
1161 * hosts above 255 and requires exactly 4 octets.
1162 */
1163int buf2ip(const char *buf, size_t len, struct in_addr *dst)
1164{
1165 const char *addr;
1166 int saw_digit, octets, ch;
1167 u_char tmp[4], *tp;
1168 const char *cp = buf;
1169
1170 saw_digit = 0;
1171 octets = 0;
1172 *(tp = tmp) = 0;
1173
1174 for (addr = buf; addr - buf < len; addr++) {
1175 unsigned char digit = (ch = *addr) - '0';
1176
1177 if (digit > 9 && ch != '.')
1178 break;
1179
1180 if (digit <= 9) {
1181 u_int new = *tp * 10 + digit;
1182
1183 if (new > 255)
1184 return 0;
1185
1186 *tp = new;
1187
1188 if (!saw_digit) {
1189 if (++octets > 4)
1190 return 0;
1191 saw_digit = 1;
1192 }
1193 } else if (ch == '.' && saw_digit) {
1194 if (octets == 4)
1195 return 0;
1196
1197 *++tp = 0;
1198 saw_digit = 0;
1199 } else
1200 return 0;
1201 }
1202
1203 if (octets < 4)
1204 return 0;
1205
1206 memcpy(&dst->s_addr, tmp, 4);
1207 return addr - cp;
1208}
1209
Willy Tarreauacf95772010-06-14 19:09:21 +02001210/* To be used to quote config arg positions. Returns the short string at <ptr>
1211 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
1212 * if ptr is NULL or empty. The string is locally allocated.
1213 */
1214const char *quote_arg(const char *ptr)
1215{
1216 static char val[32];
1217 int i;
1218
1219 if (!ptr || !*ptr)
1220 return "end of line";
1221 val[0] = '\'';
1222 for (i = 1; i < sizeof(val) - 1 && *ptr; i++)
1223 val[i] = *ptr++;
1224 val[i++] = '\'';
1225 val[i] = '\0';
1226 return val;
1227}
1228
Willy Tarreau5b180202010-07-18 10:40:48 +02001229/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
1230int get_std_op(const char *str)
1231{
1232 int ret = -1;
1233
1234 if (*str == 'e' && str[1] == 'q')
1235 ret = STD_OP_EQ;
1236 else if (*str == 'n' && str[1] == 'e')
1237 ret = STD_OP_NE;
1238 else if (*str == 'l') {
1239 if (str[1] == 'e') ret = STD_OP_LE;
1240 else if (str[1] == 't') ret = STD_OP_LT;
1241 }
1242 else if (*str == 'g') {
1243 if (str[1] == 'e') ret = STD_OP_GE;
1244 else if (str[1] == 't') ret = STD_OP_GT;
1245 }
1246
1247 if (ret == -1 || str[2] != '\0')
1248 return -1;
1249 return ret;
1250}
1251
Willy Tarreau4c14eaa2010-11-24 14:01:45 +01001252/* hash a 32-bit integer to another 32-bit integer */
1253unsigned int full_hash(unsigned int a)
1254{
1255 return __full_hash(a);
1256}
1257
David du Colombier4f92d322011-03-24 11:09:31 +01001258/* Return non-zero if IPv4 address is part of the network,
1259 * otherwise zero.
1260 */
1261int in_net_ipv4(struct in_addr *addr, struct in_addr *mask, struct in_addr *net)
1262{
1263 return((addr->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr));
1264}
1265
1266/* Return non-zero if IPv6 address is part of the network,
1267 * otherwise zero.
1268 */
1269int in_net_ipv6(struct in6_addr *addr, struct in6_addr *mask, struct in6_addr *net)
1270{
1271 int i;
1272
1273 for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++)
1274 if (((((int *)addr)[i] & ((int *)mask)[i])) !=
1275 (((int *)net)[i] & ((int *)mask)[i]))
1276 return 0;
1277 return 1;
1278}
1279
1280/* RFC 4291 prefix */
1281const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00,
1282 0x00, 0x00, 0x00, 0x00,
1283 0x00, 0x00, 0xFF, 0xFF };
1284
1285/* Map IPv4 adress on IPv6 address, as specified in RFC 3513. */
1286void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr)
1287{
1288 memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx));
1289 memcpy(sin6_addr->s6_addr+12, &sin_addr->s_addr, 4);
1290}
1291
1292/* Map IPv6 adress on IPv4 address, as specified in RFC 3513.
1293 * Return true if conversion is possible and false otherwise.
1294 */
1295int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr)
1296{
1297 if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) {
1298 memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]),
1299 sizeof(struct in_addr));
1300 return 1;
1301 }
1302
1303 return 0;
1304}
1305
Willy Tarreaubaaee002006-06-26 02:48:02 +02001306/*
1307 * Local variables:
1308 * c-indent-level: 8
1309 * c-basic-offset: 8
1310 * End:
1311 */