MINOR: tools: Add a function to convert buffer to an ipv6 address

The inet_pton function needs an input string with a final \0. This
function copies the input string to a temporary buffer, adds the final
\0 and converts to address.
This commit is contained in:
Thierry FOURNIER 2013-11-22 16:16:59 +01:00 committed by Willy Tarreau
parent 9c1d67ecbd
commit d559dd8390
2 changed files with 22 additions and 0 deletions

View File

@ -527,6 +527,7 @@ int word_match(const char *sample, int slen, const char *word, int wlen);
* or the number of chars read in case of success. * or the number of chars read in case of success.
*/ */
int buf2ip(const char *buf, size_t len, struct in_addr *dst); int buf2ip(const char *buf, size_t len, struct in_addr *dst);
int buf2ip6(const char *buf, size_t len, struct in6_addr *dst);
/* To be used to quote config arg positions. Returns the string at <ptr> /* To be used to quote config arg positions. Returns the string at <ptr>
* surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line" * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"

View File

@ -1625,6 +1625,27 @@ int buf2ip(const char *buf, size_t len, struct in_addr *dst)
return addr - cp; return addr - cp;
} }
/* This function converts the string in <buf> of the len <len> to
* struct in6_addr <dst> which must be allocated by the caller.
* This function returns 1 in success case, otherwise zero.
*/
#define MAX_IP6_LEN 45
int buf2ip6(const char *buf, size_t len, struct in6_addr *dst)
{
char null_term_ip6[MAX_IP6_LEN + 1];
if (len > MAX_IP6_LEN)
return 0;
memcpy(null_term_ip6, buf, len);
null_term_ip6[len] = '\0';
if (!inet_pton(AF_INET6, null_term_ip6, dst))
return 0;
return 1;
}
/* To be used to quote config arg positions. Returns the short string at <ptr> /* To be used to quote config arg positions. Returns the short string at <ptr>
* surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line" * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
* if ptr is NULL or empty. The string is locally allocated. * if ptr is NULL or empty. The string is locally allocated.