blob: e3717172528718a8e332ff89bab97dddfb39e363 [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 Tarreaubaaee002006-06-26 02:48:02 +0200556/* will try to encode the string <string> replacing all characters tagged in
557 * <map> with the hexadecimal representation of their ASCII-code (2 digits)
558 * prefixed by <escape>, and will store the result between <start> (included)
559 * and <stop> (excluded), and will always terminate the string with a '\0'
560 * before <stop>. The position of the '\0' is returned if the conversion
561 * completes. If bytes are missing between <start> and <stop>, then the
562 * conversion will be incomplete and truncated. If <stop> <= <start>, the '\0'
563 * cannot even be stored so we return <start> without writing the 0.
564 * The input string must also be zero-terminated.
565 */
566const char hextab[16] = "0123456789ABCDEF";
567char *encode_string(char *start, char *stop,
568 const char escape, const fd_set *map,
569 const char *string)
570{
571 if (start < stop) {
572 stop--; /* reserve one byte for the final '\0' */
573 while (start < stop && *string != '\0') {
574 if (!FD_ISSET((unsigned char)(*string), map))
575 *start++ = *string;
576 else {
577 if (start + 3 >= stop)
578 break;
579 *start++ = escape;
580 *start++ = hextab[(*string >> 4) & 15];
581 *start++ = hextab[*string & 15];
582 }
583 string++;
584 }
585 *start = '\0';
586 }
587 return start;
588}
589
Willy Tarreaubf9c2fc2011-05-31 18:06:18 +0200590/* Decode an URL-encoded string in-place. The resulting string might
591 * be shorter. If some forbidden characters are found, the conversion is
592 * aborted, the string is truncated before the issue and non-zero is returned,
593 * otherwise the operation returns non-zero indicating success.
594 */
595int url_decode(char *string)
596{
597 char *in, *out;
598 int ret = 0;
599
600 in = string;
601 out = string;
602 while (*in) {
603 switch (*in) {
604 case '+' :
605 *out++ = ' ';
606 break;
607 case '%' :
608 if (!ishex(in[1]) || !ishex(in[2]))
609 goto end;
610 *out++ = (hex2i(in[1]) << 4) + hex2i(in[2]);
611 in += 2;
612 break;
613 default:
614 *out++ = *in;
615 break;
616 }
617 in++;
618 }
619 ret = 1; /* success */
620 end:
621 *out = 0;
622 return ret;
623}
Willy Tarreaubaaee002006-06-26 02:48:02 +0200624
Willy Tarreau6911fa42007-03-04 18:06:08 +0100625unsigned int str2ui(const char *s)
626{
627 return __str2ui(s);
628}
629
630unsigned int str2uic(const char *s)
631{
632 return __str2uic(s);
633}
634
635unsigned int strl2ui(const char *s, int len)
636{
637 return __strl2ui(s, len);
638}
639
640unsigned int strl2uic(const char *s, int len)
641{
642 return __strl2uic(s, len);
643}
644
Willy Tarreau4ec83cd2010-10-15 23:19:55 +0200645unsigned int read_uint(const char **s, const char *end)
646{
647 return __read_uint(s, end);
648}
649
Willy Tarreau6911fa42007-03-04 18:06:08 +0100650/* This one is 7 times faster than strtol() on athlon with checks.
651 * It returns the value of the number composed of all valid digits read,
652 * and can process negative numbers too.
653 */
654int strl2ic(const char *s, int len)
655{
656 int i = 0;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200657 int j, k;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100658
659 if (len > 0) {
660 if (*s != '-') {
661 /* positive number */
662 while (len-- > 0) {
663 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200664 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100665 if (j > 9)
666 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200667 i = k + j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100668 }
669 } else {
670 /* negative number */
671 s++;
672 while (--len > 0) {
673 j = (*s++) - '0';
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200674 k = i * 10;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100675 if (j > 9)
676 break;
Willy Tarreau3f0c9762007-10-25 09:42:24 +0200677 i = k - j;
Willy Tarreau6911fa42007-03-04 18:06:08 +0100678 }
679 }
680 }
681 return i;
682}
683
684
685/* This function reads exactly <len> chars from <s> and converts them to a
686 * signed integer which it stores into <ret>. It accurately detects any error
687 * (truncated string, invalid chars, overflows). It is meant to be used in
688 * applications designed for hostile environments. It returns zero when the
689 * number has successfully been converted, non-zero otherwise. When an error
690 * is returned, the <ret> value is left untouched. It is yet 5 to 40 times
691 * faster than strtol().
692 */
693int strl2irc(const char *s, int len, int *ret)
694{
695 int i = 0;
696 int j;
697
698 if (!len)
699 return 1;
700
701 if (*s != '-') {
702 /* positive number */
703 while (len-- > 0) {
704 j = (*s++) - '0';
705 if (j > 9) return 1; /* invalid char */
706 if (i > INT_MAX / 10) return 1; /* check for multiply overflow */
707 i = i * 10;
708 if (i + j < i) return 1; /* check for addition overflow */
709 i = i + j;
710 }
711 } else {
712 /* negative number */
713 s++;
714 while (--len > 0) {
715 j = (*s++) - '0';
716 if (j > 9) return 1; /* invalid char */
717 if (i < INT_MIN / 10) return 1; /* check for multiply overflow */
718 i = i * 10;
719 if (i - j > i) return 1; /* check for subtract overflow */
720 i = i - j;
721 }
722 }
723 *ret = i;
724 return 0;
725}
726
727
728/* This function reads exactly <len> chars from <s> and converts them to a
729 * signed integer which it stores into <ret>. It accurately detects any error
730 * (truncated string, invalid chars, overflows). It is meant to be used in
731 * applications designed for hostile environments. It returns zero when the
732 * number has successfully been converted, non-zero otherwise. When an error
733 * is returned, the <ret> value is left untouched. It is about 3 times slower
734 * than str2irc().
735 */
736#ifndef LLONG_MAX
737#define LLONG_MAX 9223372036854775807LL
738#define LLONG_MIN (-LLONG_MAX - 1LL)
739#endif
740
741int strl2llrc(const char *s, int len, long long *ret)
742{
743 long long i = 0;
744 int j;
745
746 if (!len)
747 return 1;
748
749 if (*s != '-') {
750 /* positive number */
751 while (len-- > 0) {
752 j = (*s++) - '0';
753 if (j > 9) return 1; /* invalid char */
754 if (i > LLONG_MAX / 10LL) return 1; /* check for multiply overflow */
755 i = i * 10LL;
756 if (i + j < i) return 1; /* check for addition overflow */
757 i = i + j;
758 }
759 } else {
760 /* negative number */
761 s++;
762 while (--len > 0) {
763 j = (*s++) - '0';
764 if (j > 9) return 1; /* invalid char */
765 if (i < LLONG_MIN / 10LL) return 1; /* check for multiply overflow */
766 i = i * 10LL;
767 if (i - j > i) return 1; /* check for subtract overflow */
768 i = i - j;
769 }
770 }
771 *ret = i;
772 return 0;
773}
774
Willy Tarreaua0d37b62007-12-02 22:00:35 +0100775/* This function parses a time value optionally followed by a unit suffix among
776 * "d", "h", "m", "s", "ms" or "us". It converts the value into the unit
777 * expected by the caller. The computation does its best to avoid overflows.
778 * The value is returned in <ret> if everything is fine, and a NULL is returned
779 * by the function. In case of error, a pointer to the error is returned and
780 * <ret> is left untouched. Values are automatically rounded up when needed.
781 */
782const char *parse_time_err(const char *text, unsigned *ret, unsigned unit_flags)
783{
784 unsigned imult, idiv;
785 unsigned omult, odiv;
786 unsigned value;
787
788 omult = odiv = 1;
789
790 switch (unit_flags & TIME_UNIT_MASK) {
791 case TIME_UNIT_US: omult = 1000000; break;
792 case TIME_UNIT_MS: omult = 1000; break;
793 case TIME_UNIT_S: break;
794 case TIME_UNIT_MIN: odiv = 60; break;
795 case TIME_UNIT_HOUR: odiv = 3600; break;
796 case TIME_UNIT_DAY: odiv = 86400; break;
797 default: break;
798 }
799
800 value = 0;
801
802 while (1) {
803 unsigned int j;
804
805 j = *text - '0';
806 if (j > 9)
807 break;
808 text++;
809 value *= 10;
810 value += j;
811 }
812
813 imult = idiv = 1;
814 switch (*text) {
815 case '\0': /* no unit = default unit */
816 imult = omult = idiv = odiv = 1;
817 break;
818 case 's': /* second = unscaled unit */
819 break;
820 case 'u': /* microsecond : "us" */
821 if (text[1] == 's') {
822 idiv = 1000000;
823 text++;
824 }
825 break;
826 case 'm': /* millisecond : "ms" or minute: "m" */
827 if (text[1] == 's') {
828 idiv = 1000;
829 text++;
830 } else
831 imult = 60;
832 break;
833 case 'h': /* hour : "h" */
834 imult = 3600;
835 break;
836 case 'd': /* day : "d" */
837 imult = 86400;
838 break;
839 default:
840 return text;
841 break;
842 }
843
844 if (omult % idiv == 0) { omult /= idiv; idiv = 1; }
845 if (idiv % omult == 0) { idiv /= omult; omult = 1; }
846 if (imult % odiv == 0) { imult /= odiv; odiv = 1; }
847 if (odiv % imult == 0) { odiv /= imult; imult = 1; }
848
849 value = (value * (imult * omult) + (idiv * odiv - 1)) / (idiv * odiv);
850 *ret = value;
851 return NULL;
852}
Willy Tarreau6911fa42007-03-04 18:06:08 +0100853
Emeric Brun39132b22010-01-04 14:57:24 +0100854/* this function converts the string starting at <text> to an unsigned int
855 * stored in <ret>. If an error is detected, the pointer to the unexpected
856 * character is returned. If the conversio is succesful, NULL is returned.
857 */
858const char *parse_size_err(const char *text, unsigned *ret) {
859 unsigned value = 0;
860
861 while (1) {
862 unsigned int j;
863
864 j = *text - '0';
865 if (j > 9)
866 break;
867 if (value > ~0U / 10)
868 return text;
869 value *= 10;
870 if (value > (value + j))
871 return text;
872 value += j;
873 text++;
874 }
875
876 switch (*text) {
877 case '\0':
878 break;
879 case 'K':
880 case 'k':
881 if (value > ~0U >> 10)
882 return text;
883 value = value << 10;
884 break;
885 case 'M':
886 case 'm':
887 if (value > ~0U >> 20)
888 return text;
889 value = value << 20;
890 break;
891 case 'G':
892 case 'g':
893 if (value > ~0U >> 30)
894 return text;
895 value = value << 30;
896 break;
897 default:
898 return text;
899 }
900
901 *ret = value;
902 return NULL;
903}
904
Willy Tarreau946ba592009-05-10 15:41:18 +0200905/* copies at most <n> characters from <src> and always terminates with '\0' */
906char *my_strndup(const char *src, int n)
907{
908 int len = 0;
909 char *ret;
910
911 while (len < n && src[len])
912 len++;
913
914 ret = (char *)malloc(len + 1);
915 if (!ret)
916 return ret;
917 memcpy(ret, src, len);
918 ret[len] = '\0';
919 return ret;
920}
921
Willy Tarreau482b00d2009-10-04 22:48:42 +0200922/* This function returns the first unused key greater than or equal to <key> in
923 * ID tree <root>. Zero is returned if no place is found.
924 */
925unsigned int get_next_id(struct eb_root *root, unsigned int key)
926{
927 struct eb32_node *used;
928
929 do {
930 used = eb32_lookup_ge(root, key);
931 if (!used || used->key > key)
932 return key; /* key is available */
933 key++;
934 } while (key);
935 return key;
936}
937
Willy Tarreau348238b2010-01-18 15:05:57 +0100938/* This function compares a sample word possibly followed by blanks to another
939 * clean word. The compare is case-insensitive. 1 is returned if both are equal,
940 * otherwise zero. This intends to be used when checking HTTP headers for some
941 * values. Note that it validates a word followed only by blanks but does not
942 * validate a word followed by blanks then other chars.
943 */
944int word_match(const char *sample, int slen, const char *word, int wlen)
945{
946 if (slen < wlen)
947 return 0;
948
949 while (wlen) {
950 char c = *sample ^ *word;
951 if (c && c != ('A' ^ 'a'))
952 return 0;
953 sample++;
954 word++;
955 slen--;
956 wlen--;
957 }
958
959 while (slen) {
960 if (*sample != ' ' && *sample != '\t')
961 return 0;
962 sample++;
963 slen--;
964 }
965 return 1;
966}
Willy Tarreau482b00d2009-10-04 22:48:42 +0200967
Willy Tarreaud54bbdc2009-09-07 11:00:31 +0200968/* Converts any text-formatted IPv4 address to a host-order IPv4 address. It
969 * is particularly fast because it avoids expensive operations such as
970 * multiplies, which are optimized away at the end. It requires a properly
971 * formated address though (3 points).
972 */
973unsigned int inetaddr_host(const char *text)
974{
975 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
976 register unsigned int dig100, dig10, dig1;
977 int s;
978 const char *p, *d;
979
980 dig1 = dig10 = dig100 = ascii_zero;
981 s = 24;
982
983 p = text;
984 while (1) {
985 if (((unsigned)(*p - '0')) <= 9) {
986 p++;
987 continue;
988 }
989
990 /* here, we have a complete byte between <text> and <p> (exclusive) */
991 if (p == text)
992 goto end;
993
994 d = p - 1;
995 dig1 |= (unsigned int)(*d << s);
996 if (d == text)
997 goto end;
998
999 d--;
1000 dig10 |= (unsigned int)(*d << s);
1001 if (d == text)
1002 goto end;
1003
1004 d--;
1005 dig100 |= (unsigned int)(*d << s);
1006 end:
1007 if (!s || *p != '.')
1008 break;
1009
1010 s -= 8;
1011 text = ++p;
1012 }
1013
1014 dig100 -= ascii_zero;
1015 dig10 -= ascii_zero;
1016 dig1 -= ascii_zero;
1017 return ((dig100 * 10) + dig10) * 10 + dig1;
1018}
1019
1020/*
1021 * Idem except the first unparsed character has to be passed in <stop>.
1022 */
1023unsigned int inetaddr_host_lim(const char *text, const char *stop)
1024{
1025 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1026 register unsigned int dig100, dig10, dig1;
1027 int s;
1028 const char *p, *d;
1029
1030 dig1 = dig10 = dig100 = ascii_zero;
1031 s = 24;
1032
1033 p = text;
1034 while (1) {
1035 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1036 p++;
1037 continue;
1038 }
1039
1040 /* here, we have a complete byte between <text> and <p> (exclusive) */
1041 if (p == text)
1042 goto end;
1043
1044 d = p - 1;
1045 dig1 |= (unsigned int)(*d << s);
1046 if (d == text)
1047 goto end;
1048
1049 d--;
1050 dig10 |= (unsigned int)(*d << s);
1051 if (d == text)
1052 goto end;
1053
1054 d--;
1055 dig100 |= (unsigned int)(*d << s);
1056 end:
1057 if (!s || p == stop || *p != '.')
1058 break;
1059
1060 s -= 8;
1061 text = ++p;
1062 }
1063
1064 dig100 -= ascii_zero;
1065 dig10 -= ascii_zero;
1066 dig1 -= ascii_zero;
1067 return ((dig100 * 10) + dig10) * 10 + dig1;
1068}
1069
1070/*
1071 * Idem except the pointer to first unparsed byte is returned into <ret> which
1072 * must not be NULL.
1073 */
Willy Tarreau74172752010-10-15 23:21:42 +02001074unsigned int inetaddr_host_lim_ret(char *text, char *stop, char **ret)
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001075{
1076 const unsigned int ascii_zero = ('0' << 24) | ('0' << 16) | ('0' << 8) | '0';
1077 register unsigned int dig100, dig10, dig1;
1078 int s;
Willy Tarreau74172752010-10-15 23:21:42 +02001079 char *p, *d;
Willy Tarreaud54bbdc2009-09-07 11:00:31 +02001080
1081 dig1 = dig10 = dig100 = ascii_zero;
1082 s = 24;
1083
1084 p = text;
1085 while (1) {
1086 if (((unsigned)(*p - '0')) <= 9 && p < stop) {
1087 p++;
1088 continue;
1089 }
1090
1091 /* here, we have a complete byte between <text> and <p> (exclusive) */
1092 if (p == text)
1093 goto end;
1094
1095 d = p - 1;
1096 dig1 |= (unsigned int)(*d << s);
1097 if (d == text)
1098 goto end;
1099
1100 d--;
1101 dig10 |= (unsigned int)(*d << s);
1102 if (d == text)
1103 goto end;
1104
1105 d--;
1106 dig100 |= (unsigned int)(*d << s);
1107 end:
1108 if (!s || p == stop || *p != '.')
1109 break;
1110
1111 s -= 8;
1112 text = ++p;
1113 }
1114
1115 *ret = p;
1116 dig100 -= ascii_zero;
1117 dig10 -= ascii_zero;
1118 dig1 -= ascii_zero;
1119 return ((dig100 * 10) + dig10) * 10 + dig1;
1120}
1121
Willy Tarreauf0b38bf2010-06-06 13:22:23 +02001122/* Convert a fixed-length string to an IP address. Returns 0 in case of error,
1123 * or the number of chars read in case of success. Maybe this could be replaced
1124 * by one of the functions above. Also, apparently this function does not support
1125 * hosts above 255 and requires exactly 4 octets.
1126 */
1127int buf2ip(const char *buf, size_t len, struct in_addr *dst)
1128{
1129 const char *addr;
1130 int saw_digit, octets, ch;
1131 u_char tmp[4], *tp;
1132 const char *cp = buf;
1133
1134 saw_digit = 0;
1135 octets = 0;
1136 *(tp = tmp) = 0;
1137
1138 for (addr = buf; addr - buf < len; addr++) {
1139 unsigned char digit = (ch = *addr) - '0';
1140
1141 if (digit > 9 && ch != '.')
1142 break;
1143
1144 if (digit <= 9) {
1145 u_int new = *tp * 10 + digit;
1146
1147 if (new > 255)
1148 return 0;
1149
1150 *tp = new;
1151
1152 if (!saw_digit) {
1153 if (++octets > 4)
1154 return 0;
1155 saw_digit = 1;
1156 }
1157 } else if (ch == '.' && saw_digit) {
1158 if (octets == 4)
1159 return 0;
1160
1161 *++tp = 0;
1162 saw_digit = 0;
1163 } else
1164 return 0;
1165 }
1166
1167 if (octets < 4)
1168 return 0;
1169
1170 memcpy(&dst->s_addr, tmp, 4);
1171 return addr - cp;
1172}
1173
Willy Tarreauacf95772010-06-14 19:09:21 +02001174/* To be used to quote config arg positions. Returns the short string at <ptr>
1175 * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
1176 * if ptr is NULL or empty. The string is locally allocated.
1177 */
1178const char *quote_arg(const char *ptr)
1179{
1180 static char val[32];
1181 int i;
1182
1183 if (!ptr || !*ptr)
1184 return "end of line";
1185 val[0] = '\'';
1186 for (i = 1; i < sizeof(val) - 1 && *ptr; i++)
1187 val[i] = *ptr++;
1188 val[i++] = '\'';
1189 val[i] = '\0';
1190 return val;
1191}
1192
Willy Tarreau5b180202010-07-18 10:40:48 +02001193/* returns an operator among STD_OP_* for string <str> or < 0 if unknown */
1194int get_std_op(const char *str)
1195{
1196 int ret = -1;
1197
1198 if (*str == 'e' && str[1] == 'q')
1199 ret = STD_OP_EQ;
1200 else if (*str == 'n' && str[1] == 'e')
1201 ret = STD_OP_NE;
1202 else if (*str == 'l') {
1203 if (str[1] == 'e') ret = STD_OP_LE;
1204 else if (str[1] == 't') ret = STD_OP_LT;
1205 }
1206 else if (*str == 'g') {
1207 if (str[1] == 'e') ret = STD_OP_GE;
1208 else if (str[1] == 't') ret = STD_OP_GT;
1209 }
1210
1211 if (ret == -1 || str[2] != '\0')
1212 return -1;
1213 return ret;
1214}
1215
Willy Tarreau4c14eaa2010-11-24 14:01:45 +01001216/* hash a 32-bit integer to another 32-bit integer */
1217unsigned int full_hash(unsigned int a)
1218{
1219 return __full_hash(a);
1220}
1221
David du Colombier4f92d322011-03-24 11:09:31 +01001222/* Return non-zero if IPv4 address is part of the network,
1223 * otherwise zero.
1224 */
1225int in_net_ipv4(struct in_addr *addr, struct in_addr *mask, struct in_addr *net)
1226{
1227 return((addr->s_addr & mask->s_addr) == (net->s_addr & mask->s_addr));
1228}
1229
1230/* Return non-zero if IPv6 address is part of the network,
1231 * otherwise zero.
1232 */
1233int in_net_ipv6(struct in6_addr *addr, struct in6_addr *mask, struct in6_addr *net)
1234{
1235 int i;
1236
1237 for (i = 0; i < sizeof(struct in6_addr) / sizeof(int); i++)
1238 if (((((int *)addr)[i] & ((int *)mask)[i])) !=
1239 (((int *)net)[i] & ((int *)mask)[i]))
1240 return 0;
1241 return 1;
1242}
1243
1244/* RFC 4291 prefix */
1245const char rfc4291_pfx[] = { 0x00, 0x00, 0x00, 0x00,
1246 0x00, 0x00, 0x00, 0x00,
1247 0x00, 0x00, 0xFF, 0xFF };
1248
1249/* Map IPv4 adress on IPv6 address, as specified in RFC 3513. */
1250void v4tov6(struct in6_addr *sin6_addr, struct in_addr *sin_addr)
1251{
1252 memcpy(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx));
1253 memcpy(sin6_addr->s6_addr+12, &sin_addr->s_addr, 4);
1254}
1255
1256/* Map IPv6 adress on IPv4 address, as specified in RFC 3513.
1257 * Return true if conversion is possible and false otherwise.
1258 */
1259int v6tov4(struct in_addr *sin_addr, struct in6_addr *sin6_addr)
1260{
1261 if (memcmp(sin6_addr->s6_addr, rfc4291_pfx, sizeof(rfc4291_pfx)) == 0) {
1262 memcpy(&(sin_addr->s_addr), &(sin6_addr->s6_addr[12]),
1263 sizeof(struct in_addr));
1264 return 1;
1265 }
1266
1267 return 0;
1268}
1269
Willy Tarreaubaaee002006-06-26 02:48:02 +02001270/*
1271 * Local variables:
1272 * c-indent-level: 8
1273 * c-basic-offset: 8
1274 * End:
1275 */