ipv6_string_mapped stores an IPv6 or IPv4 representation of an IPv6 address into
a string. If the address is IPv4-mapped, it's represented in IPv4 dotted-decimal
format.

Signed-off-by: Thadeu Lima de Souza Cascardo <casca...@redhat.com>
---
 lib/packets.c | 15 +++++++++++++++
 lib/packets.h |  1 +
 2 files changed, 16 insertions(+)

diff --git a/lib/packets.c b/lib/packets.c
index f6fd480..c790087 100644
--- a/lib/packets.c
+++ b/lib/packets.c
@@ -474,6 +474,21 @@ ipv6_format_masked(const struct in6_addr *addr, const 
struct in6_addr *mask,
     }
 }
 
+/* Stores the string representation of the IPv6 address 'addr' into the
+ * character array 'addr_str', which must be at least INET6_ADDRSTRLEN
+ * bytes long. If addr is IPv4-mapped, store an IPv4 dotted-decimal string. */
+const char *
+ipv6_string_mapped(char *addr_str, const struct in6_addr *addr)
+{
+    ovs_be32 ip;
+    ip = in6_addr_get_mapped_ipv4(addr);
+    if (ip) {
+        return inet_ntop(AF_INET, &ip, addr_str, INET6_ADDRSTRLEN);
+    } else {
+        return inet_ntop(AF_INET6, addr, addr_str, INET6_ADDRSTRLEN);
+    }
+}
+
 struct in6_addr ipv6_addr_bitand(const struct in6_addr *a,
                                  const struct in6_addr *b)
 {
diff --git a/lib/packets.h b/lib/packets.h
index 188cf84..d5277ab 100644
--- a/lib/packets.h
+++ b/lib/packets.h
@@ -954,6 +954,7 @@ void ipv6_format_addr(const struct in6_addr *addr, struct 
ds *);
 void ipv6_format_mapped(const struct in6_addr *addr, struct ds *);
 void ipv6_format_masked(const struct in6_addr *addr,
                         const struct in6_addr *mask, struct ds *);
+const char * ipv6_string_mapped(char *addr_str, const struct in6_addr *addr);
 struct in6_addr ipv6_addr_bitand(const struct in6_addr *src,
                                  const struct in6_addr *mask);
 struct in6_addr ipv6_create_mask(int mask);
-- 
2.5.0

_______________________________________________
dev mailing list
dev@openvswitch.org
http://openvswitch.org/mailman/listinfo/dev

Reply via email to