]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: tools: Add a function to convert buffer to an ipv6 address
authorThierry FOURNIER <tfournier@exceliance.fr>
Fri, 22 Nov 2013 15:16:59 +0000 (16:16 +0100)
committerWilly Tarreau <w@1wt.eu>
Mon, 2 Dec 2013 22:31:32 +0000 (23:31 +0100)
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
src/standard.c

index df061b25a1f6f1837cee45fee9691a043829b486..8c5871f64c3f7b1390a28ed39fcdca4b69e9aac3 100644 (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.
  */
 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>
  * surrounded by simple quotes if <ptr> is valid and non-empty, or "end of line"
index 9f7f939fd860fecc967ca7306fecd2d263de3b24..41359c9c840b428a67c5a5b00ec98a36dbb0c449 100644 (file)
@@ -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 <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>
  * 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.