From d559dd8390f70e1ff5afc89750afbfb94ff00bb0 Mon Sep 17 00:00:00 2001 From: Thierry FOURNIER Date: Fri, 22 Nov 2013 16:16:59 +0100 Subject: [PATCH] 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. --- include/common/standard.h | 1 + src/standard.c | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/include/common/standard.h b/include/common/standard.h index df061b25a..8c5871f64 100644 --- a/include/common/standard.h +++ b/include/common/standard.h @@ -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. */ 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 * surrounded by simple quotes if is valid and non-empty, or "end of line" diff --git a/src/standard.c b/src/standard.c index 9f7f939fd..41359c9c8 100644 --- a/src/standard.c +++ b/src/standard.c @@ -1625,6 +1625,27 @@ int buf2ip(const char *buf, size_t len, struct in_addr *dst) return addr - cp; } +/* This function converts the string in of the len to + * struct in6_addr 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 * surrounded by simple quotes if is valid and non-empty, or "end of line" * if ptr is NULL or empty. The string is locally allocated.