diff mbox

[ovs-dev,v2,03/14] Add ipv6_string_mapped.

Message ID 1448458277-28154-4-git-send-email-cascardo@redhat.com
State Accepted
Headers show

Commit Message

Thadeu Lima de Souza Cascardo Nov. 25, 2015, 1:31 p.m. UTC
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 <cascardo@redhat.com>
---
 lib/packets.c | 15 +++++++++++++++
 lib/packets.h |  1 +
 2 files changed, 16 insertions(+)

Comments

Ben Pfaff Nov. 30, 2015, 6:10 p.m. UTC | #1
On Wed, Nov 25, 2015 at 11:31:06AM -0200, Thadeu Lima de Souza Cascardo wrote:
> 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 <cascardo@redhat.com>

Applied, thanks!
diff mbox

Patch

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);